Extracting multiple lists out of a nested dictionary
<p>I am new to Python. I want to extract multiple lists out of the nested dictionary. I did follow this link (<a href="http://stackoverflow.com/questions/28131446/get-nested-arrays-out-of-a-dictionary">Get nested arrays out of a dictionary</a>) didn't help me much.</p>

<p>If I have a dictionary, say:</p>

<pre><code>{"alpha": {2: {1: 1.1, 2: 4.1}, 3: {1: 9.1, 3: 4.1, 6: 5.1},
           5: {1: 9.2, 3: 4.4, 6: 5.4}, 9: {1: 9.0, 3: 4.0, 6: 5.5}},
 "beta": {2: {1: 4.0, 2: 7.9}, 3: {1: 24, 3: 89, 6: 98} ,
          5: {1: 9, 3: 4, 6: 5}, 9: {1: 9.2, 3: 4.9, 6: 5.0}}}
</code></pre>

<p>How do I extract all these as individual lists say <code>(alpha,beta,..), (2,3,5,9), (1,2,4,9), (1.1,4.1)</code> etc.</p>

<p>When I tried I could get only  the list of (alpha,beta,..) and list of values associated with alpha and beta. The list of values is again a dictionary as it is inside a list. I can't further do dict.values() as the previous operation gave me a list. Therefore, Python throws an error. How do I make list of all these values and keys individually? Like I want to make a list of decimals and the keys associated with that.</p>

<p>You can get each 'layer' by accessing <code>dict.keys()</code> or get the layer below by accessing <code>dict.values()</code>. If you want to dive one level deeper you just iterate over <code>parent.values()</code> and get <code>dict.keys()</code> on each element. The last layer finally is just <code>dict.values()</code>.</p>

<pre><code>print data.keys()                                                # top level keys
&gt;&gt;&gt; ['alpha', 'beta']

print [x.keys() for x in data.values()]                          # second level keys
&gt;&gt;&gt; [[9, 2, 3, 5], [9, 2, 3, 5]]

print [y.keys() for x in data.values() for y in x.values()]      # third level keys
&gt;&gt;&gt; [[1, 3, 6], [1, 2], [1, 3, 6], [1, 3, 6], [1, 3, 6], [1, 2], [1, 3, 6], [1, 3, 6]]

print [y.values() for x in data.values() for y in x.values()]    # third level values
&gt;&gt;&gt; [[9.0, 4.0, 5.5], [1.1, 4.1], [9.1, 4.1, 5.1], [9.2, 4.4, 5.4], [9.2, 4.9, 5.0], [4.0, 7.9], [24, 89, 98], [9, 4, 5]]
</code></pre>

<p>Note that dicts are unordered by nature.</p>

python list dictionary