Reverse zip() ?

Stefan Behnel stefan_ml at behnel.de
Tue Dec 2 21:12:19 CET 2008


Andreas Waldenburger wrote:
> we all know about the zip builtin that combines several iterables into
> a list of tuples.
> 
> I often find myself doing the reverse, splitting a list of tuples into
> several lists, each corresponding to a certain element of each tuple
> (e.g. matplotlib/pyplot needs those, rather than lists of points).
> 
> This is of course trivial to do via iteration or listcomps, BUT, I was
> wondering if there is a function I don't know about that does this
> nicely?

I think you're asking about zip():

	>>> l=[1,2,3]
	>>> zip(l,l)
	[(1, 1), (2, 2), (3, 3)]
	>>> zip(*zip(l,l))
	[(1, 2, 3), (1, 2, 3)]

Stefan



More information about the Python-list mailing list