Lorenzo Castagno
Lorenzo Castagno

Reputation: 572

Remove keys from a nested dict (Python keys)

I'm pretty new in Python, thanks in advance for your help.

I built the following code (I tried the below, I used a dictionary within a dictionary).

The idea is to keep the keys (hair.color) with values(blonde). In this example: remove Micheal.

Code:

def answers(hair_questions):
    try:
        for i in people:
            if people[i]["hair.color"]==hair_questions:
                print(people[i])
            else:
                del people[i]
            return people[i]
    except:
        print("Doesn´t exist")

answers("brown")

On People:

people={
 "Anne":
    {
   "gender":"female",
   "skin.color":"white",
  "hair.color":"blonde",
  "hair.shape":"curly"
 }
,
"Michael":
{

  "citizenship":"africa",
  "gender":"male",
  "hair.color":"brown",
  "hair.shape":"curly"


}
,

"Ashley":
    {
  "gender":"female",
  "citizenship":"american",
  "hair.color":"blonde",
  "hair.shape":"curly "
 }

 }

The code only check the first key: under the condition: values(blonde) i.e. (people[i]["hair.color"]!=brown) it works just for 1 key and then the code gets "stuck"

My current output:

"people"=

 "Michael":
{

  "citizenship":"africa",
  "gender":"male",
  "hair.color":"brown",
  "hair.shape":"curly"


}
,

"Ashley":
    {
  "gender":"female",
  "citizenship":"american",
  "hair.color":"blonde",
  "hair.shape":"curly "
 }

Instead, I wanted:

"people"=

"Michael":
{

  "citizenship":"africa",
  "gender":"male",
  "hair.color":"brown",
  "hair.shape":"curly"

} 

I want an output, for this case, (only) Michael.

Upvotes: 2

Views: 142

Answers (2)

ncica
ncica

Reputation: 7206

you can use list comprehension:

brown = {key:value for key,value in people.items() if people[key]["hair.color"] != "blonde"}
print (brown)

what is equal to:

brown= {}
for key,value in people.items():
    if people[key]["hair.color"] != "blonde":
        brown[key] = value
print (brown)

output:

{'Michael': {'citizenship': 'africa', 'gender': 'male', 'hair.color': 'brown', 'hair.shape': 'curly'}}

Upvotes: 2

bharatk
bharatk

Reputation: 4315

You can't delete key while iterating for loop:

people={
    "Anne":
        {
       "gender":"female",
       "skin.color":"white",
      "hair.color":"blonde",
      "hair.shape":"curly"
     },
    "Michael":
    {
      "citizenship":"africa",
      "gender":"male",
      "hair.color":"brown",
      "hair.shape":"curly"
    },
    "Ashley":
        {
          "gender":"female",
          "citizenship":"american",
          "hair.color":"blonde",
          "hair.shape":"curly "
        }
 }

def answers(hair_questions):
    my_dict = {}
    for i in people:
        if people[i]["hair.color"] in hair_questions:
            my_dict[i] = people[i]
    return  my_dict

print(answers("brown"))

OR

def answers(hair_questions):
    my_list = []
    for i in people:
        if people[i]["hair.color"] not in hair_questions:
            my_list.append(i)

    for i in my_list:
        del people[i]

answers("brown")
print(people)

O/P:

{'Michael': {'citizenship': 'africa', 'gender': 'male', 'hair.color': 'brown', 'hair.shape': 'curly'}}

Upvotes: 3

Related Questions