Python zip() function

In this post, we will see about Python zip() function.
Python zip makes an iterator that aggregates elements from each of the iterables.



It returns

  • If no argument is provided, it returns empty iterator
  • If single argument is provided, it returns iterator of 1-tuple
  • If multiple arguments are provided, it returns iterator where ith tuple has ith element from the each of iterables or sequence.The iterator stops when shortest iterable is completed.


Let’s see a simple example:

#Empty iterator print("Empty iterator:",list(zip())) #Single iterator a=[1,2,3] print("zip of a is:",list(zip(a))) #Multiple iterator with same length x=[1,3,5] y=[2,4,6] print("zip of x and y is:",list(zip(x,y))) #Multiple iterator with different length l1=[1,2] l2=[3,4,5] l3=[6,7,8,9] print("zip of l1,l2 and l3 is:",list(zip(l1,l2,l3)))


Empty iterator: []
zip of a is: [(1,), (2,), (3,)]
zip of x and y is: [(1, 2), (3, 4), (5, 6)]
zip of x and y is: [(1, 3, 6), (2, 4, 7)]

You should use zip with unequal iterator length only when you don’t care about trailing elements in long iterators.


If you want to unzip tuple, you can use * with zip.Please refer below example.

x=[(1,2),(3,4),(5,6)] x1,x2=zip(*x) print(x1) print(x2)


(1, 3, 5)
(2, 4, 6)

That’s all about zip function in python.

Leave a Reply

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