Learn Python Zip Function with Syntax and Examples

In this article, we will focus on the details of the Python zip() function and see it’s applications.

We will cover the syntax of the function, how to zip() items from multiple iterables into one iterable and in the end we will also learn to unzip the zip.

Python Zip() Function

The zip() is a built-in function which means that it is available to use anywhere in the code. It is used to map similar index items into a single entity. It can take zero or more arguments and it returns us an iterator to the tuple.

Syntax:

zip(*iterables)

Python Zip() Parameters

Python zip function takes built-in or user-defined iterables like lists, strings, dictionary, etc.

Let’s see this with an example.

Code:

print( zip() )
print( zip([1,2,3], [1,2,4] ))

Output:

<zip object at 0x000002C61C827188>
<zip object at 0x000002C61C827188>

We can see that the function returns us an iterator to a tuple object. So we can use a for loop to iterate over all the items which have been zipped.

Python Zip() Function Examplespython zip function examples

1. No Arguments

When no arguments are passed to the Python zip() function then the zip() returns an iterator to an empty tuple.

Code:

print(list(zip()))

Output:

[]

As you can see we got an empty list when we converted the iterator into a list.

2. Single Argument

When a single iterable argument is passed to the zip() function, the result is zipped in an iterator. We will use a for loop to see all the items.

Code:

for i in zip([1,2,3,4,5]):
  print(i)

Output:

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

Each item of the iterable argument is converted into a tuple.

3. Multiple Arguments of Same Length

The main purpose of Python zip() function is seen when we pass multiple arguments of the same length. As a result, we will get an iterator to the items of the same index mapped in a tuple.

You will clearly understand this by looking at the example.

Code:

student = [‘Shrangi’, ‘Akshay’, ‘Aman’ , ‘Joy’]
book_id = [1969, 2453, 2189, 1865 ]
price = [1100, 780, 800, 950]

for record in zip(student, book_id, price):
  print(record)

Output:

(‘Shrangi’, 1969, 1100)
(‘Akshay’, 2453, 780)
(‘Aman’, 2189, 800)
(‘Joy’, 1865, 950)

4. Multiple Arguments of Different Length

We saw multiple arguments of the same length but what will happen to the index when different length arguments are passed.

Let’s see a quick example.

Code:

for i in zip([1,2,3,4],[1,2,3],[1,2,3,4,5]):
  print(i)

Output:

(1, 1, 1)
(2, 2, 2)
(3, 3, 3)

Here we see that the zip() function will only map items from left to right.

If one of the iterable in arguments is exhausted then it doesn’t yield further.

Unzipping Values in Python

Till now we discussed zipping the values together. Now, let’s see how we can unzip them.

For unzipping values we use the * operator it is similar to the tuples unpacking.

Code:

name = [“Amanda”, “Joey”, “Sheldon”, “Rebecca”]
id = [1,2,3,4]

#zipping values
record = list( zip(name, id) )
print(record)

#unzipping values
names, ids = zip(*record)
print(names)
print(ids)

Output:

[(‘Amanda’, 1), (‘Joey’, 2), (‘Sheldon’, 3), (‘Rebecca’, 4)]
(‘Amanda’, ‘Joey’, ‘Sheldon’, ‘Rebecca’)
(1, 2, 3, 4)

Summary

In this Python zip function tutorial, we discussed the inbuilt zip() function of Python. We learnt how we can map similar indexes items of different iterables into a single entity.

We first saw the syntax of the function and then saw the behaviour of the function with a different number of arguments. At last, we learned how to unzip the zipped values.