Skip to main content

Unpacking argument lists in Python

What I want to achieve is to transpose the list of list or tupple:

original = [('First', 1, 3, 4), ('Second', 4, 5, 6)]


transposed = [('First', 'Second'), (1, 4), (3, 5), (4, 6)]

With itertools.izip I can solve my problem:

from itertools import izip
transposed = izip(original[0], original[1])

But, what if my original list contains 1000 lists/tuples? Fortunately, in Python, you can unpack the list as arguments. So, what I need to do is just:

transposed = izip(*original)

Pretty amazing huh?