Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the inverse function of zip in python? [duplicate]

People also ask

What is the opposite of zip in Python?

unzip() just opposite of zip(), means convert the zipped values back to their initial form and this done with the help of the '*' operator.

What is zip (* matrix in Python?

zip() in Python Python zip() method takes iterable or containers and returns a single iterator object, having mapped values from all the containers. It is used to map the similar index of multiple containers so that they can be used just using a single entity. Syntax : zip(*iterators)

What does Python zip return?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

What does zip (* List do in Python?

Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.


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