Transpose/Unzip Function (Inverse of Zip)

Transpose/Unzip Function (inverse of zip)?

zip is its own inverse! Provided you use the special * operator.

>>> zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)])
[('a', 'b', 'c', 'd'), (1, 2, 3, 4)]

The way this works is by calling zip with the arguments:

zip(('a', 1), ('b', 2), ('c', 3), ('d', 4))

… except the arguments are passed to zip directly (after being converted to a tuple), so there's no need to worry about the number of arguments getting too big.

What is the inverse function of zip in python?

lst1, lst2 = zip(*zipped_list)

should give you the unzipped list.

*zipped_list unpacks the zipped_list object. it then passes all the tuples from the zipped_list object to zip, which just packs them back up as they were when you passed them in.

so if:

a = [1,2,3]
b = [4,5,6]

then zipped_list = zip(a,b) gives you:

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

and *zipped_list gives you back

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

zipping that with zip(*zipped_list) gives you back the two collections:

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

Python: why is zip(*) used instead of unzip()?

You're not actually unzipping when you do zip(*your_list). You're still zipping.

zip is a function that can take as many arguments as you want. In your case, you essentially have four different sequences that you want to zip: ('a', 1), ('b', 2), ('c', 3) and ('d', 4). Thus, you want to call zip like this:

>>> zip(('a', 1), ('b', 2), ('c', 3), ('d', 4))
[('a', 'b', 'c', 'd'), (1, 2, 3, 4)]

But your sequences aren't in separate variables, you just have a list which contains them all. This is where the * operator comes in. This operator unpacks the list in a way that each element of your list becomes an argument to the function.

This means that when you do this:

your_list = [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
zip(*your_list)

Python calls zip which each element of your list as an argument, like this:

zip(('a', 1), ('b', 2), ('c', 3), ('d', 4))

This is why an unzip function isn't necessary: Unzipping is just another kind of zip, and is easily achievable with just the zip function and the * operator.

How to unzip a list of tuples into individual lists?

Use zip(*list):

>>> l = [(1,2), (3,4), (8,9)]
>>> list(zip(*l))
[(1, 3, 8), (2, 4, 9)]

The zip() function pairs up the elements from all inputs, starting with the first values, then the second, etc. By using *l you apply all tuples in l as separate arguments to the zip() function, so zip() pairs up 1 with 3 with 8 first, then 2 with 4 and 9. Those happen to correspond nicely with the columns, or the transposition of l.

zip() produces tuples; if you must have mutable list objects, just map() the tuples to lists or use a list comprehension to produce a list of lists:

map(list, zip(*l))          # keep it a generator
[list(t) for t in zip(*l)] # consume the zip generator into a list of lists

Python zip list of tuples

Just use zip and list comprehension.

>>> tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
>>> [list(i) for i in zip(*tuples)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Or,

>>> tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
>>> [[*i] for i in zip(*tuples)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Creating a function that works like zip

Try this one.

def zip_copy(*args):
lst = []
i = 0
t = tuple()
for a in range(len(min(args,key=len))):
for a in args:
t+=(a[i],)
i+=1
lst +=(t,)
t = tuple()
return lst
print(zip_copy([1,2],[3,4],[3,5])) # [(1, 3, 3), (2, 4, 5)]
print(list(zip([1,2],[3,4],[3,5]))) # [(1, 3, 3), (2, 4, 5)]
# ------------------------------------#
print(zip_copy([1,2])) # [(1,), (2,)]
print(list(zip([1,2]))) # [(1,), (2,)]
# ------------------------------------ #
print(zip_copy("Hello","Hii")) # [('H', 'H'), ('e', 'i'), ('l', 'i')]
print(list(zip("Hello","Hii"))) # [('H', 'H'), ('e', 'i'), ('l', 'i')]


Related Topics



Leave a reply



Submit