majid bhatti
majid bhatti

Reputation: 103

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?

Upvotes: 1

Views: 1354

Answers (3)

Stuart
Stuart

Reputation: 9868

You could make your own flattener function which yields the values in a dict and members of a list:

def is_nonstring_iterable(x):
    return ((hasattr(x, "__iter__") or hasattr(x, "__getitem__")) 
        and not isinstance(x, str))

def flatten(thing):
    """ Recursively iterate through values in dictionary-like object or members 
        in lists, tuples or other non-string iterables """
    if hasattr(thing, "values"):
        thing = thing.values()
    if is_nonstring_iterable(thing):
        for element in thing:
            yield from flatten(element)
    else:
        yield thing

for d in [d1, d2, d3]:
    print(list(flatten(d)))
    print(any(item == "hello" for item in flatten(d))) # returns whether hello is found

Upvotes: 0

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10970

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

Upvotes: 3

Alireza Tajadod
Alireza Tajadod

Reputation: 327

You can keep going inside the dictionary until you are at a value, not a dictionary. Recursion would be helpful here:

def get_value(dict):
     dict_value = list(dict.values())[0]
     if type(dict_value) is dict:
          get_value(dict_value)
     else: 
          return dict_value

there is probably a nicer way to do dict_value = list(dict.values())[0] but not coming to mind right now

Upvotes: 0

Related Questions