keys and values lining up
Guido van Rossum
guido at cnri.reston.va.us
Wed Sep 29 08:26:43 EDT 1999
Michael Haggerty <mhagger at blizzard.harvard.edu> writes:
> There's always
>
> >>> d = {'key1' : 'val1', 'key2' : 'val2', 'key3' : 'val3'}
> >>> apply(map, [None] + d.items())
> [('key1', 'key2', 'key3'), ('val1', 'val2', 'val3')]
>
> if you have to go the items() route.
I don't know how well that will work for a really large dictionary,
because all arguments get pushed on the stack (not the C stack, but
still...).
But it's a really cool trick (by which I meant I hadn't tought of it
:-). Might be worth noting in the Python book of idioms.
Of course, if you have NumPy, there's always
keys, values = transpose(d.items())
which probably beats everything else.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-list
mailing list