Using zip in Python

Photo by Tomas Sobek on Unsplash

Using zip in Python

If you haven't encountered it already, note that zip is quite handy in Python.

It allows you to go through multiple iterables (such as lists, sets, tuples etc) at the same time, effectively "zipping" them into an iterator that will produce tuples of elements from each initial collection.

Worth keeping in mind that if the provided iterables' length differs, zip will stop when the shortest of them has reached its end.

Happy coding!

integers = [1,2,3,4,5]

letters = ['a','b', 'c', 'd']

for pair in zip(integers, letters):

# Prints:
# (1, 'a')
# (2, 'b')
# (3, 'c')
# (4, 'd')