making zip with list comprehensions
Ian McMeans
imcmeans at home.com
Tue Jul 9 05:56:36 EDT 2002
I was idly trying to replicate zip's (the function) behavior using list
comprehensions, but I couldn't do it.
Can anyone? I came up with these, but they're ugly.
>>> [(x,y) for x in ['a','b','c'] for y in ['d','e','f'] if
['a','b','c'].index(x) == ['d','e','f'].index(y)]
[('a', 'd'), ('b', 'e'), ('c', 'f')]
This is ugly because it loops far too many times =)
>>> [ (['a','b','c'][x], ['d','e','f'][x]) for x in range(3)]
[('a', 'd'), ('b', 'e'), ('c', 'f')]
This isn't so bad. Does anyone have better suggestions?
More information about the Python-list
mailing list