If I have a dictionary of dictionaries of arbitrary depth, how can I list all the keys that are in the dictionary at a given depth? or get a list of keys in the dictionary and their depth?
For example, a simple dictionary:
dict_o_dicts = {'a': {1:'bob', 2: 'fred', 3: 'henry'},
'b': {2:'fred',3: 'henry', 4: 'pascale'}
}
and I need a team that does something like:
print keys_at_depth(dict_o_dicts, 0)
will return: ['a', 'b']
and
print keys_at_depth(dict_o_dicts, 1)
will return [1,2,3,4]
I can recursively go through the dictionary to find the maximum depth of the dictionary, but as soon as I try to report both the depth and the key values, I will ultimately break the recursion.
thanks
source
share