If you blog it they will come?

Thursday, September 17, 2009

Zip those lists

A lot of people know about zip() in python but did you know it operates on a variable argument list?


a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

>>zip(a, b, c)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]


Don't roll your own list transposition functions....
It's also useful for iterating over a couple of related lists:


for message, email in (messages, emails):
hdrs, body = parse_email(email)
assert message in body