>>> x = [1, 2, 3] >>> y = [4, 5, 6] >>> zipped = zip(x, y) >>> list(zipped) [(1, 4), (2, 5), (3, 6)] >>> x2, y2 = zip(*zip(x, y)) >>> x == list(x2) and y == list(y2) True
Here is what the above code is Doing:
1. zip() in Python3 returns an iterator.
2. zip() can accept any type of iterable, like files, lists, tuples, sets, and dictionaries.
3. zip() returns an iterator of tuples.
4. If you do list(zipped), it will unzip the list.
5. The asterisk * performs tuple unpacking.
6. The zip() function can accept an arbitrary number of iterables.
7. The zip() function stops when the shortest iterable is exhausted.