Title: How do I turn a dictionary with lists as values into a list of dictionaries with single values?
Tags: python;dictionary
Question: I have this dict:
'x': [0,1,2],
'y': ['a','b','c']
A dictionary where all the values are lists, of identical length.
I want to produce this:
{ 'x': 0, 'y': 'a' },
{ 'x': 1, 'y': 'b' },
{ 'x': 2, 'y': 'c' }
Is there an efficient way to do this? Hopefully using something in itertools?

Here is the accepted answer: [dict(zip(d, vals)) for vals in zip(*d.values())]
For example:
&gt;&gt;&gt; d = {'y': ['a', 'b', 'c'], 'x': [0, 1, 2]}
&gt;&gt;&gt; [dict(zip(d, vals)) for vals in zip(*d.values())]
[{'y': 'a', 'x': 0}, {'y': 'b', 'x': 1}, {'y': 'c', 'x': 2}]

Here is another answer: 'x': [0,1,2],
'y': ['a','b','c']
values = zip(*d.values())
print [dict(zip(d.keys(), v)) for v in values]

Here is another answer: [{'x': v, 'y': d['y'][i]} for i, v in enumerate(d['x'])]
Comment for this answer: This works only in the very specific case of 'x' and 'y' being the keys, when that was probably only an example (there might be more than two keys, and the keys might not be known in advance).

Here is another answer: [dict(stuff) for stuff in zip(*[[(k, v) for v in vs] for k, vs in myDict.iteritems()])]

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:07/18/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><dictionary>', 'Title': 'How do I turn a dictionary with lists as values into a list of dictionaries with single values?', 'ViewCount': 82, 'id': 11534733, 'user_AboutMe': '<p>Experienced full-stack developer with a passion for the inner workings of systems. I like dancing, cycling, gardening &amp; politics.</p>\n', 'user_AccountId': 146005.0, 'user_DisplayName': 'Cera', 'user_Reputation': 1819.0, 'user_Views': 247.0, 'user_id': 356191.0}}