How to Use zip() Function in Python

The zip() function take iterables (can be zero or more), makes iterator that aggregates elements based on the iterables passed, and returns an iterator of tuples.

Example 1:

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]

xyz = zip(x, y, z)
print xyz

The result is:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Example 2:

x = [1, 2, 3]
y = [4, 5, 6, 7]
xy = zip(x, y)
print xy

The result is:

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

Example 3:

x = [1, 2, 3]
x = zip(x)
print x

The result is:

[(1,), (2,), (3,)]

Example 4:

x = zip()
print x

The result is:

[]

,