a question about zip...

Diez B. Roggisch deets at nospam.web.de
Wed Mar 8 15:41:58 EST 2006


KraftDiner schrieb:
> I had a structure that looked like this
> ((0,1), (2, 3), (4, 5), (6,7)
> 
> I changed my code slightly and now I do this:
> odd = (1,3,5,7)
> even = (0,2,4,6)
> all = zip(even, odd)
> 
> however the zip produces:
> [(0, 1), (2, 3), (4, 5), (6, 7)]
> 
> Which is a list of tuples.. I wanted a tuple of tuples...

Use tuple(zip(...))

There is a reason that zip returns a list: appending to a list is considered a "natural" operation, whereas extending an 
immutable(!) tuple creates a _new_ tuple for each entry. Which is a comparable costly operation - actually quadratic 
complexity instead of linear.

So - if you need a tuple afterwards, convert it as shown above.

Diez



More information about the Python-list mailing list