Iterating over a nested dictionary
<p>Purpose this code that works to iterate over a <code>nested dictionary</code> but I'm looking for an output that gives a tuple or <code>list</code> of [keys] and then [values]. Here's the code:</p>

<pre><code>from collections import Mapping, Set, Sequence

string_types = (str, unicode) if str is bytes else (str, bytes)
iteritems = lambda mapping: getattr(mapping, 'iteritems', mapping.items)()

def recurse(obj, path=(), memo=None):
    if memo is None:
        memo = set()
    iterator = None
    if isinstance(obj, Mapping):
        iterator = iteritems
    elif isinstance(obj, (Sequence, Set)) and not isinstance(obj, string_types):
        iterator = enumerate
    if iterator:
        if id(obj) not in memo:
            memo.add(id(obj))
            for path_component, value in iterator(obj):
                for result in recurse(value, path + (path_component,), memo):
                    yield result
            memo.remove(id(obj))
    else:
        yield path, obj


class addNestedDict(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

loansDict=addNestedDict()
loansDict[15]['A']['B']=[1,2,3,4]

for k,v in recurse(loansDict):
    print(k,v)
</code></pre>

<p>The output I'm looking for is one line <code>(15 ,'A','B') [1,2,3,4]</code> so that I can be able to reference <code>k[0]</code>,<code>k[1]</code> and <code>v[0]</code> etc...</p>

<p>This seems to work:</p>

<pre><code>results = AddNestedDict()
for k,v in recurse(loansDict):
    results.setdefault(k[:-1], []).append(v)
result_key, result_value = results.items()[0]
print('{} {}'.format(result_key, result_value))  # -&gt; (15, 'A', 'B') [1, 2, 3, 4]
</code></pre>

<p>I renamed your class <code>AddNestedDict</code> so it follows <a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP 8</a> guidelines.</p>

python python-3.x