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.
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)]))