Zipping and rezipping lists

I think a common mistake when coming to Python from some other programming languages is to loop through a list like this:

for i in range(len(my_list)):
    print my_list[i]

Rather than:

for x in my_list:
    print x

Whilst I knew not to do this, when trying to loop through two lists at the same time, I found myself resorting to this:

for i, x in enumerate(list1):
    print x, list2[i]

A much better solution is to zip the two lists together like this:

for (i, j) in zip(list1, list2):
    print i, j

A syntax that confused me for some time was zip(*my_list):

z = zip(list1, list2)
newlist1, newlist2 = zip(*z)

This works becauses the * syntax unpacks a list of values. The above code zips and unzips two lists, which is pointless, but the same syntax can be used to convert from a list of columns of data to a list of rows of data. For example, the following list comprehension reads in a file of tab-delimited data as a list of rows, where each row is a tuple of values:

rows = [line.rstrip().split('\t') for line in file(filename)]

If you want to flip the data through 90 degrees (i.e. convert from rows or data to columns of data), then you use:

columns = zip(*rows)

For example, if the data was originally (a, 1), (b, 2), (c, 3), it becomes (a, b, c), (1, 2, 3).