Q:

Nested lists and dictionaries in python

I have 3 different dictionaries in python like these
d1 = {'simple_key':'hello'}
d2 = {'k1':{'k2':'hello'}}
d3 = {'k1':[{'nest_key':['this is deep',['hello']]}]}

and I want to grab the word 'hello' from these dictionaries. 
But the problem I am facing is that I want to find a generic way to extract it. How can I do it?

A:

To get the key path in the dict using value, you could flatten it as json.
>>> from json_flatten import flatten
>>> d1 = {'simple_key':'hello'}
>>> d2 = {'k1':{'k2':'hello'}}
>>> d3 = {'k1':[{'nest_key':['this is deep',['hello']]}]}
>>> flatten(d2)
{'k1.k2': 'hello'}
>>> flat = flatten(d3)
{'k1.0.nest_key.0': 'this is deep', 'k1.0.nest_key.1.0': 'hello'}

To find the matching keys, use,
>>> [k for k, v in flat.items() if v == 'hello']
['k1.0.nest_key.1.0']

JSON Flatten

