简体   繁体   中英

How do I iterate through nested dictionaries in a list of dictionaries?

Still new to Python and need a little help here. I've found some answers for iterating through a list of dictionaries but not for nested dictionaries in a list of dictionaries.

Here is the a rough structure of a single dictionary within the dictionary list

[{ 'a':'1',
'b':'2',
'c':'3',
'd':{ 'ab':'12',
      'cd':'34',
      'ef':'56'},
'e':'4',
'f':'etc...'
}]



dict_list = [{ 'a':'1', 'b':'2', 'c':'3', 'd':{ 'ab':'12','cd':'34', 'ef':'56'}, 'e':'4', 'f':'etc...'}, { 'a':'2', 'b':'3', 'c':'4', 'd':{ 'ab':'23','cd':'45', 'ef':'67'}, 'e':'5', 'f':'etcx2...'},{},........,{}]

That's more or less what I am looking at although there are some keys with lists as values instead of a dictionary but I don't think I need to worry about them right now although code that would catch those would be great.

Here is what I have so far which does a great job of iterating through the json and returning all the values for each 'high level' key.

import ujson as json

with open('test.json', 'r') as f:
    json_text = f.read()

dict_list = json.loads(json_text)

for dic in dict_list:
    for val in dic.values():
        print(val)

Here is the first set of values that are returned when that loop runs

1
2
3
{'ab':'12','cd':'34','ef':'56'}
4
etc...

What I need to be able to do pick specific values from the top level and go one level deeper and grab specific values in that nested dictionary and append them to a list(s). I'm sure I am missing a simple solution. Maybe I'm looking at multiple loops?

How about checking for the instance type using isinstance (of course only works for one level deeper). Might not be the best way though

for dic in dict_list:
    for val in dic.values():
        if not isinstance(val, dict):
            print(val)
        else:    
            for val2 in val.values():
                print (val2)

# 1
# 2
# 3
# 12
# 34
# 56
# 4
# etc...
# 2
# 3

Following the ducktype style encouraged with Python, just guess everything has a .values member, and catch it if they do not:

import ujson as json

with open('test.json', 'r') as f:
    json_text = f.read()

dict_list = json.loads(json_text)

for dic in dict_list:
    for val in dic.values():
        try:
            for l2_val in val.values():
                print(l2_val)
        except AttributeError:
            print(val)

Bazingaa's solution would be faster if inner dictionaries are expected to be rare.

Of course, any more "deep" and you would need some recursion probably:

def print_dict(d):
    for val in d.values():
       try:
           print_dict(val)
        except AttributeError:
           print(val)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM