convert list of tuples into several lists

Nick Craig-Wood nick at craig-wood.com
Thu Feb 10 05:30:01 EST 2005


Cappy2112 <cappy2112 at gmail.com> wrote:
>  What does the leading * do?

It causes the list/tuple following the * to be unpacked into function
arguments. Eg

>>> zip(*[(1, 2, 3), (4, 5, 6)])
[(1, 4), (2, 5), (3, 6)]

is the same as

>>> zip((1, 2, 3), (4, 5, 6))
[(1, 4), (2, 5), (3, 6)]

The * should make you think of dereferencing things (eg pointer
de-reference in C).

Its equivalent to the now deprecated apply function which does the
same thing in a more wordy fashion, ie apply the list as parameters to
the function.

>>> apply(zip, [(1, 2, 3), (4, 5, 6)])
[(1, 4), (2, 5), (3, 6)]

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list