for i,j in zip(a,b):
print (i, j)
# zip multiple lists
names = [ ' tim ', ' joe ' , ' billy ', ' sally ' ]
ages = [ 21, 19, 18, 43 ]
eye_color = [ ' blue ' , ' brown ' , ' brown ' , ' green ']
print(list(zip(names, ages, eye_color))) # return list of tuples [ ( ' tim ', 21 , ' blue ' ), ( ' joe ' , 19 , ' brown '), ( ' billy ', 18, ' brown ' ), ( ' sally ', 43 , ' green ' )]
for name, age in zip( names, ages ) :
if age > 20 :
print(name) # tim sally
"""
Joining any number of iterables by combining elements in order
- Iterables include: str, list, tuples, dict etc...
- No error will be incurred if you zip lists of differing lengths,...
...it will simply zip up to the length of the shortest list
"""
lst1 = [1, 2, 3, 4, 5, 7]
lst2 = "mangos"
lst3 = (3.1, 5.4, 0.2, 23.2, 8.88, 898)
lst4 = {"Car": "Mercedes Benz", "Location": "Eiffel Tower", "Organism": "Tardigrade"}
# lst5, lst6, ...
result = list(zip(lst1, lst2, lst3, lst4.keys())) # Check out dictionary methods
print(result)
## [(1, 'm', 3.1, 'Car'), (2, 'a', 5.4, 'Location'), (3, 'n', 0.2, 'Organism')]
d = {'k1': 1, 'k2': 2}
# d.update(k3=3, k3=300)
# SyntaxError: keyword argument repeated: k3
d = {'k1': 1, 'k2': 2}
d.update([('k3', 3), ('k3', 300)])
print(d)
# {'k1': 1, 'k2': 2, 'k3': 300}
d = {'k1': 1, 'k2': 2}
keys = ['k3', 'k3']
values = [3, 300]
d.update(zip(keys, values))
print(d)
# {'k1': 1, 'k2': 2, 'k3': 300}