Created
April 18, 2017 00:16
-
-
Save nasturtus/273706f6491eb2199634ac1a89632d65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zip it up! | |
# Reference: | |
# https://pythonprogramming.net/list-comprehension-generators-intermediate-python-tutorial/ | |
a = [10, 20, 30] | |
b = [40, 50, 60] | |
print(zip(a, b)) | |
print(list(zip(a, b))) | |
print() | |
names = ['Jay', 'Joe', 'Poe'] | |
ages = [10, 40, 70, 100] | |
print(zip(names, ages)) | |
print(dict(zip(names, ages))) # value '100' from ages is ignored | |
print() | |
# zip in list comprehension without saving output to a list variable | |
[print (name, age) for name, age in zip(names, ages)] | |
print() | |
# zip in list comprehension plus saving output to a list variable | |
lst = [(name, age) for name, age in zip(names, ages)] | |
print(lst) | |
print() | |
# zip in for-loop | |
for name, age in zip(names, ages): | |
print("{}: {}".format(name, age)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment