-1

I am very new to coding and I am trying to access a key(such as 'name') that is within a nested dictionary that has a list which holds all of the Dictionaries.

Ex. I want to access the Sex of the dictionary of people.

This is the code:

people = {1: [{"name": 'John'}, {'Age': '27'}, {'Sex': 'Male'}],
          2: [{"name": 'Marie'}, {'Age': '22'}, {'Sex': 'Female'}],
              }

for i, x in people.items():
    Accessing_People_List = people[i]
    print(people[i])
    print(type(people[i]))
    print(Accessing_People_List[i])

So far I could only access till the list part, after that everything went as well as a cook trying to do surgery on a live person.(no offense)

So could yall give me some or any suggestions on accessing it and explain how that code to does that? (cause this is some sort of a practice for myself ) GLHF.

TL;DR: need help to access a key of a nested dictionary with a list that holds several dictionaries Edit: Btw, thanks for asking

  • Dictionaries can have more than one pair of values. You should use something like `{"name": 'John', 'Age': '27', 'Sex': 'Male'}` for your inner object instead. – Miguel Guthridge Jul 11 '22 at 07:44
  • The reason why you are having such problems is because your object structure makes no sense. Your inner lists contains 3 separate dictionaries with single key each. Drop the lists, make them 1 dictionary. - `1: {"name": 'John', 'Age': '27', 'Sex': 'Male'},` – matszwecja Jul 11 '22 at 07:45

2 Answers2

1

The reason why you are having such problems is because your object structure makes no sense. Your inner lists contains 3 separate dictionaries with single key each. Drop the lists, make them 1 dictionary. -

1: {"name": 'John', 'Age': '27', 'Sex': 'Male'}

Now you can access it like this: people[1]["Sex"]. Or even better make name the key:

people = {"John": {'Age': '27', 'Sex': 'Male'},
          "Marie": {'Age': '22', 'Sex': 'Female'},
              }

print(people["John"]["Sex"])

Should you want to get it from your structure, you'd have to do something like this:

people[1][2]['Sex']

2 shows up because it dictionary that contains this info is index 2 element of the list.

matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • Although in this context my code seems abit weird, but can you elaborate on why my object structure makes no sense? Btw thx for answering – Yes Idk How To Code Jul 11 '22 at 09:52
  • Well, you have the lists which give you no additional functionality and 1 key-value in each dictionary which defeats their purpose. It makes no sense exactly because of things I mentioned. It is problematic to use and kills the benefits of the dictionary. – matszwecja Jul 11 '22 at 10:00
0

Assuming your dict is given and that you cannot change it by hand, you could do something like this to get the desired structure and then access each key:

from collections import ChainMap

people = {1: [{"name": 'John'}, {'Age': '27'}, {'Sex': 'Male'}],
          2: [{"name": 'Marie'}, {'Age': '22'}, {'Sex': 'Female'}],
              }

new_dict = {key: dict(ChainMap(*people[key])) for key in people}

new_dict[1]["Sex"] # -> 'Male'

If you do not want to import ChainMap, you can also use the following:

people = {1: [{"name": 'John'}, {'Age': '27'}, {'Sex': 'Male'}],
          2: [{"name": 'Marie'}, {'Age': '22'}, {'Sex': 'Female'}],
              }

new_dict = {key: {k:v for d in people[key] for k, v in d.items()} for key in people}

new_dict[1]["Sex"] # -> 'Male'
ko3
  • 1,757
  • 5
  • 13
  • Btw, firstly u r a life saver. Secondly why do you import chainmap for this code, is it too complicated/long if u were to manually create everything? – Yes Idk How To Code Jul 11 '22 at 09:55
  • @YesIdkHowToCode, cheers my friend. If you do not want to import `ChainMap`, you can decompose your list of dicts into one dict by an additional dict-comprehension: `new_dict = {key: {k:v for d in people[key] for k, v in d.items()} for key in people}`. I have updated my answer for you. – ko3 Jul 11 '22 at 10:02