zip in Python

While trying to learn more about list comprehension in Python, I came across a very useful function called zip.

Zip combines two or more iterables into an iterable consisting of tuples. Tuple on index n has elements having index n from all the iterables passed to the zip function.

Example:

x = [1, 2, 3]
y = [2, 4, 6]
for i, j in zip(x, y):
    print(i + j, end = ' ')      

Output: 3 6 9

Leave a comment

Your email address will not be published. Required fields are marked *