Nested dictionary and comprehension in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Nested dictionary and comprehension in python

i have a nested dictionary in the following form: dict = {"0":{ }, "1":{ }, "2":{ }, "3":{ }, ... } the inner dictionaries have various keys, not necessarily uniform, eg: dict["0"] = {"name" : "bruce", "age" : 28, ...} dict["1"] = {"name" : "eve", "sex" : "female", ...} I'm interested in creating a one-liner that decides if a specific name is present or not, eg: name_check = True if "bruce" in dict[<any>]["name"] I understand you can't access nested dicts like this, but is there a simple way w/o loops?

9th May 2018, 10:31 AM
rdhelli
rdhelli - avatar
4 Answers
11th May 2018, 12:58 AM
E_E Mopho
E_E Mopho - avatar
+ 1
w/o means without, so even though your code does what I asked, it's not what I'm looking for :/ can you think of how to do this with no loops?
12th May 2018, 6:16 AM
rdhelli
rdhelli - avatar
+ 1
you should do this with loops unless you are working with really small dicts. you could use keys as index numbers for the data to use less resources with looping.
12th May 2018, 2:00 PM
Markus Kaleton
Markus Kaleton - avatar
0
rdhelli Edited. Check again
12th May 2018, 1:27 PM
E_E Mopho
E_E Mopho - avatar