I have a dictionary of lists, and I want to combine them into a single namedtuples list. I want the first element of all lists in the first tuple, the second in the second, etc.
Example:
{'key1': [1, 2, 3], 'key2': [4, 5, 6], 'key3': [7, 8, 9]}
And I want the resulting list to look like this:
[('key1': 1, 'key2': 4, 'key3': 7),
('key1': 2, 'key2': 5, 'key3': 8),
('key1': 3, 'key2': 6, 'key3': 9)]
I guess there is an elegant way to do this?
Edit:
I compared the running time of @Steve Jessop with the name tuple to the dictionary version by @Ashwini Chaudhary, and the first is somewhat faster:
d = {key: numpy.random.random_integers(0, 10000, 100000)
for key in ['key1', 'key2', 'key3']}
Avg. from 100 runs:
namedtuple and map: 0.093583753109
namedtuple and zip: 0.119455988407
dictionary and zip: 0.159063346386