I have a python dictionary consisting of JSON results. The dictionary contains a nested dictionary, which contains a nested list which contains a nested dictionary. Still with me? Here’s an example:
{'hits':{'results':[{'key1':'value1',
'key2':'value2',
'key3':{'sub_key':'sub_value'}},
{'key1':'value3',
'key2':'value4',
'key3':{'sub_key':'sub_value2'}}
]}}
What I want to get from the dictionary is the sub_vale of each sub_key and store it in a different list. No matter what I try I keep getting errors.
This was my last attempt at it:
inner_list=mydict['hits']['results']#This is the list of the inner_dicts
index = 0
for x in inner_list:
new_dict[index] = x[u'sub_key']
index = index + 1
print new_dict
It printed the first few results then started to return everything in the original dictionary. I can’t get my head around it. If I replace the new_dict[index] line with a print statement it prints to the screen perfectly. Really need some input on this!
for x in inner_list:
print x[u'sub_key']
After making some assumptions:
To change every instance: