Python Forum

Full Version: Newbie question for unzip zipped list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello:
I have some code, but I want to do the reverse:
list1 = [1,2,3,4]
list2 = [2,3,4,5]
list3 = [3,4,5,6]
list4 = [4,5,6,7]
zip4List = zip(list1,list2,list3,list4)
The fact is: I already have a zipped list which was generated by 4 lists zipped together, but I want to get each of the list from the zipped one, in my above example, I want to get the list1, list2, list3 and list4.
I search around, it seems python has only zip function, but no unzip function.
Any idea how to code to unzip the above zipped list?
Thanks,
a, b, c, d = zip(*zip4List)
That will get you tuples, but if you really need lists, you can call list() on them.
What does zipped list mean? zip(*args) returns a zip object which is either iterator or generator. Don't know exactly. So, zip4list is not a list of lists. In order to get one is a little change.

zip4list = [list(item) for item in zip(list1,list2,list3,list4)]