Skip to content

Instantly share code, notes, and snippets.

@nedbat
Created April 13, 2016 15:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedbat/f09c9767067e4b5c3bca01e369d1a4d7 to your computer and use it in GitHub Desktop.
Save nedbat/f09c9767067e4b5c3bca01e369d1a4d7 to your computer and use it in GitHub Desktop.
Demonstration of zip being its own inverse
Python 3.5.1 (default, Dec 7 2015, 06:25:20)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = list(range(5))
>>> b = list(range(100, 105))
>>> a
[0, 1, 2, 3, 4]
>>> b
[100, 101, 102, 103, 104]
>>> ab = list(zip(a, b))
>>> ab
[(0, 100), (1, 101), (2, 102), (3, 103), (4, 104)]
>>> a, b = zip(*ab)
>>> a
(0, 1, 2, 3, 4)
>>> b
(100, 101, 102, 103, 104)
>>>
>>> abt = list(zip(*ab))
>>> abt
[(0, 1, 2, 3, 4), (100, 101, 102, 103, 104)]
>>> ab2 = list(zip(*abt))
>>> ab2
[(0, 100), (1, 101), (2, 102), (3, 103), (4, 104)]
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment