Zip with a list comprehension

Steven Bethard steven.bethard at
Fri Dec 10 21:54:40 CET 2004

Matt Gerrans wrote:
> This is probably so easy that I'll be embarrassed by the answer.   While 
> enhancing and refactoring some old code, I was just changing some map()s to 
> list comprehensions, but I couldn't see any easy way to change a zip() to a 
> list comprehension.    Should I just let those sleeping dogs lie?   (list 
> comprehensions seem more readable than map(), but if the list comprehension 
> that does the equivalent of zip() is less expressive than zip(), I'll stick 
> with zip()).

Hmmm...  I couldn't do any better than:

 >>> seqs = [(1, 2, 3), (4, 5, 6, 7)]
 >>> zip(*seqs)
[(1, 4), (2, 5), (3, 6)]
 >>> [tuple(seq[i] for seq in seqs)
...  for i in range(min(len(seq) for seq in seqs))]
[(1, 4), (2, 5), (3, 6)]

I think I'll stick with zip. ;)

I too have been trying to make my code more conformant with Python 3000 
recommendations (e.g. removing maps in favor of LCs or GEs, replacing 
lambdas with named functions, etc.) but I've left zip pretty much alone.


More information about the Python-list mailing list