Zip with a list comprehension

Raymond Hettinger vze4rx4y at verizon.net
Sat Dec 11 06:08:07 EST 2004


> 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()).

Leave in zip(), enumerate(), and reversed().
They are meant to be used with listcomps and genexps.

If you want to go the extra distance, itertools.izip() can offer a performance
boost and better memory utilization than zip().  It can be used almost anywhere
as long as the app doesn't demand a real list.


Raymond Hettinger





More information about the Python-list mailing list