Skip to content

Instantly share code, notes, and snippets.

@bolerap
Created November 3, 2016 04:50
Show Gist options
  • Save bolerap/2ba6195c0cb95aad3ea58f9add05c66c to your computer and use it in GitHub Desktop.
Save bolerap/2ba6195c0cb95aad3ea58f9add05c66c to your computer and use it in GitHub Desktop.
# zipping: python 3 return a zip object, python 2 return a list of tuples
a = [1, 2, 3]
b = ['a', 'b', 'c']
zipped = zip(a, b) # This result will only live for once used then zipped will become empty
# to print value of zip
# 1 use for loop as a iterator
for x in zipped:
print(x) # (1, 'a') ...
# 2 convert to list
zipped = list(zipped)
print(zipped) # [(1, 'a'), (2, 'b'), (3, 'c')]
# unzipping
# 1
x, y = zip(*zip(a, b))
# 2
zipped = zip(a, b)
x, y = zip(*zipped)
print(x, y) # (1, 2, 3) ('a', 'b', 'c')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment