unzip() just opposite of zip(), means convert the zipped values back to their initial form and this done with the help of the '*' operator.
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)
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.
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With