keys and values lining up
M.-A. Lemburg
mal at lemburg.com
Wed Sep 29 16:29:04 EDT 1999
Guido van Rossum wrote:
>
> 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.
Or maybe the lists() and tuples() function in mxTools (available from my
Python Pages):
>>> d = {'key1' : 'val1', 'key2' : 'val2', 'key3' : 'val3'}
>>> import NewBuiltins # provided by mxTools
>>> lists(d.items())
(['key1', 'key2', 'key3'], ['val1', 'val2', 'val3'])
>>> tuples(d.items())
[('key1', 'key2', 'key3'), ('val1', 'val2', 'val3')]
depends on what you want to do with the result... (and yes
tuples() and lists() is written in C too).
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 93 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
More information about the Python-list
mailing list