+2 votes
in Programming Languages by (6k points)
How can I unzip a list of tuples {e.g.  a=[(1,2),(3,4),(5,6)]} into individual lists {e.g. [[1, 3, 5], [2, 4, 6]]}?

I tried unzip command, but got the following error: "NameError: name 'unzip' is not defined"

1 Answer

0 votes
by (11.3k points)

I do not think there exists a unzip() function. You should try the following:

zip(*list)

It will create the original list, but in tuple format. You can map tuple to list. Here is an example.

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

...