How to convert list of tuples to multiple lists?

xiaohan2012 picture xiaohan2012 · Nov 10, 2011 · Viewed 55.9k times · Source

Suppose I have a list of tuples and I want to convert to multiple lists.

For example, the list of tuples is

[(1,2),(3,4),(5,6),]

Is there any built-in function in Python that convert it to:

[1,3,5],[2,4,6]

This can be a simple program. But I am just curious about the existence of such built-in function in Python.

Answer

Sven Marnach picture Sven Marnach · Nov 10, 2011

The built-in function zip() will almost do what you want:

>>> zip(*[(1, 2), (3, 4), (5, 6)])
[(1, 3, 5), (2, 4, 6)]

The only difference is that you get tuples instead of lists. You can convert them to lists using

map(list, zip(*[(1, 2), (3, 4), (5, 6)]))