for k, v in d.iteritems():
if type(v) is dict:
for t, c in v.iteritems():
print \"{0} : {1}\".format(t, c)
I\'m trying to l
Since a dict
is iterable, you can apply the classic nested container iterable formula to this problem with only a couple of minor changes. Here's a Python 2 version (see below for 3):
import collections
def nested_dict_iter(nested):
for key, value in nested.iteritems():
if isinstance(value, collections.Mapping):
for inner_key, inner_value in nested_dict_iter(value):
yield inner_key, inner_value
else:
yield key, value
Test:
list(nested_dict_iter({'a':{'b':{'c':1, 'd':2},
'e':{'f':3, 'g':4}},
'h':{'i':5, 'j':6}}))
# output: [('g', 4), ('f', 3), ('c', 1), ('d', 2), ('i', 5), ('j', 6)]
In Python 2, It might be possible to create a custom Mapping
that qualifies as a Mapping
but doesn't contain iteritems
, in which case this will fail. The docs don't indicate that iteritems
is required for a Mapping
; on the other hand, the source gives Mapping
types an iteritems
method. So for custom Mappings
, inherit from collections.Mapping
explicitly just in case.
In Python 3, there are a number of improvements to be made. As of Python 3.3, abstract base classes live in collections.abc
. They remain in collections
too for backwards compatibility, but it's nicer having our abstract base classes together in one namespace. So this imports abc
from collections
. Python 3.3 also adds yield from
, which is designed for just these sorts of situations. This is not empty syntactic sugar; it may lead to faster code and more sensible interactions with coroutines.
from collections import abc
def nested_dict_iter(nested):
for key, value in nested.items():
if isinstance(value, abc.Mapping):
yield from nested_dict_iter(value)
else:
yield key, value