I have nested dictionaries:
{\'key0\': {\'attrs\': {\'entity\': \'p\', \'hash\': \'34nj3h43b4n3\', \'id\': \'4130\'},
u\'key1\': {\'attrs\': {\'ent
Since recursion is known to be limited in python (see What is the maximum recursion depth in Python, and how to increase it?) I would rather have a loop based answer to this question, so the answer can be adapted to any level of depth in the dictionary. For that, the function
def walkDict( aDict, visitor, path=() ):
for k in aDict:
if k == 'attrs':
visitor( path, aDict[k] )
elif type(aDict[k]) != dict:
pass
else:
walkDict( aDict[k], visitor, path+(k,) )
Can be replaced by:
def walkDictLoop(aDict, visitor, path=()):
toProcess = [(aDict, path)]
while toProcess:
dictNode, pathNode = toProcess.pop(0)
for k in dictNode:
if k == 'attrs':
visitor(pathNode, dictNode[k])
if isinstance(dictNode[k], dict):
toProcess.append( (dictNode[k], pathNode+(k,)) )