Posts Tagged ‘unzip’
unzip: perform the opposite of zip
March 8, 2018
Leave a comment
zip
>>> a = [1, 2, 3] >>> b = ["one", "two", "three"] >>> zip(a, b) <zip object at 0x7fd30310b508> >>> list(zip(a, b)) [(1, 'one'), (2, 'two'), (3, 'three')]
unzip
How to perform the opposite of zip? That is, we have [(1, 'one'), (2, 'two'), (3, 'three')]
, and we want to get back [1, 2, 3]
and ["one", "two", "three"]
.
>>> li [(1, 'one'), (2, 'two'), (3, 'three')] >>> a, b = zip(*li) >>> a (1, 2, 3) >>> b ('one', 'two', 'three')
Notice that the results are tuples.
More info here.