How do I turn a dictionary with lists as values into a list of dictionaries with single values?
dictionary, python
Solution
[dict(zip(d, vals)) for vals in zip(*d.values())]
For example:
>>> d = {'y': ['a', 'b', 'c'], 'x': [0, 1, 2]}
>>> [dict(zip(d, vals)) for vals in zip(*d.values())]
[{'y': 'a', 'x': 0}, {'y': 'b', 'x': 1}, {'y': 'c', 'x': 2}]
Problem
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`?