How to unpack a dictionary of list (of dictionaries!) and return as grouped tuples?
<p>I have a data structure consisting of mixed dictionaries and lists. I am trying to unpack this so as to get tuples of the keys and all sub-values for each key.</p>

<p>I am working with list comprehensions, but just not getting it to work. Where am I going wrong?</p>

<p>I saw many other answers about unpacking a list-of-lists (e.g. <a href="https://stackoverflow.com/questions/24586757/understanding-list-comprehension-for-flattening-list-of-lists-in-python">1</a>,<a href="https://stackoverflow.com/questions/8399386/nested-lists-and-list-comprehensions">2</a>), but could not find an example where a single key unpacks against multiple sub-values.</p>

<ul>
<li>desired output --> [('A',1,2),('B',3,4)]</li>
<li>actual output -->  [('A',1), ('A',2), ('B',3), ('B',4)]</li>
</ul>

<p>code:</p>

<pre><code>dict_of_lists = {'A':[{'x':1},{'x':2}], 'B':[{'x':3},{'x':4}] }
print [(key,subdict[subkey],) for key in dict_of_lists.keys() for subdict in dict_of_lists[key] for subkey in subdict.keys()]
</code></pre>