Register Login

Zip() Function in Python with Explained Examples

Updated May 03, 2020

Zip Function Python

In Python, the purpose of zip() function is to take iterable items as input and return an iterator of tuples. For example, if multiple iterables are passed, then the first item in each passed iterator is paired together and likewise. If there is no iterable items, python zip function will return an empty iterator.

If there are two iterators with different lengths, then the iterator with the least elements will be the length of the new iterator.

Suppose we passed two iterables, one iterable containing five items and the second iterable containing seven items, then the python zip() function will return iterator containing five tuples.

Zip() Function in Python

Syntax

zip(iter1, iter2, iter3 ...)

Parameters

  • Python iterables or containers ( list, string, tuple etc.)

Return Value

  • Returns a single iterator object

Python Code to Explain the Working of zip() Function

Example

# Python code to explain working of zip() function

# initializing lists
employee = [ "Ram", "Shyam", "Harish", "Ramesh" ]
employee_id = [ 1, 4, 23, 21 ]
salary = [ 40000, 50000, 60000, 70000 ]

# zip() to create zip object
zipobj = zip(employee, employee_id, salary)

# Converting values to set
zipobj = set(zipobj)

# Print ziped object
print ("The zipped Object: ",zipobj)

Output

The zipped Object: {('Ramesh', 21, 70000), ('Harish', 23, 60000), ('Ram', 1, 40000), (Shyam, 4, 50000)}

Explanation

In the above example, we have initialized 3 list namely employee, employee_id, and salary. Then in the next line, we passed these list as a parameter for the zip() function. The zip() function then returns an iterable object which we stored in the variable zipobj. To read the items of zipobj we converted it into set and later printed it.

zip() with for Loop

Example

# Python code to explain working of zip() function with for loop

# initializing lists
employee = [ "Ram", "Sham", "Harish", "Ramesh" ]
employee_id = [ 1, 4, 23, 21 ]
salary = [ 40000, 50000, 60000, 70000 ]

# zip() to create zip object
zipobj = zip(employee, employee_id, salary)

# print value using for loop

for emp, eid, esal in zipobj:
print ("Employee : ", emp, " ID: ", eid, "Salary: ", esal)

Output

Employee : Ram ID: 1 Salary: 40000 Employee : Sham ID: 4 Salary: 50000 Employee : Harish ID: 23 Salary: 60000 Employee : Ramesh ID: 21 Salary: 70000

Explanation

In the above example, we have initialized 3 variables emp, eid, esal which will help us in iterating through the for loop. Inside the for loop we used print statement which prints the items of the zipobj one by one.

zip() with Different Number of Elements in Iterables

If the zip() function passes with iterator with different length, then the return object length will be decided based on which iterator having fewer items.

Example

# Python code to explain working of zip() function with different iterator length

# initializing lists
employee = [ "Ram", "Sham", "Harish", "Ramesh" ]
employee_id = [ 1, 4, 23, 21 ]
salary = [ 40000, 50000, 60000 ]

# zip() to create zip object
zipobj = zip(employee, employee_id, salary)

# print value using for loop

for emp, eid, esal in zipobj:
print ("Employee : ", emp, " ID: ", eid, "Salary: ", esal)

Output

Employee : Ram ID: 1 Salary: 40000 Employee : Sham ID: 4 Salary: 50000 Employee : Harish ID: 23 Salary: 60000

Explanation

In the above example, we passed three list, where one of them is having 3 items and rest are having 4 items each. In this case, the returned iterator object will contain only 3 items. This is because iterator stops iterating when the shortest list is encountered. 

Unzipping the Zipped Value with Python zip() Function

In this following example, we will unzip the zipped values used in the previous example with the help of zip() function.

# Python code to explain working of zip() function

# initializing lists
employee = [ "Ram", "Sham", "Harish", "Ramesh" ]
employee_id = [ 1, 4, 23, 21 ]
salary = [ 40000, 50000, 60000, 70000 ]

# zip() to create zip object
zipobj = zip(employee, employee_id, salary)

# Convert zipped bject as set
zipobj = set(zipobj)

# Print Set
print("The zipped Output: ", zipobj)

# Unzipped the values in different list
e_name, e_id, e_salary = zip(*zipobj)

# Print lists
print("Employee Name: ", e_name)
print("Employee ID", e_id)
print("Employee Salary", e_salary)

Output

The zipped Output: {('Harish', 23, 60000), ('Ram', 1, 40000), ('Sham', 4, 50000), ('Ramesh', 21, 70000)} 
The unzipped Output: 
Employee Name: ('Harish', 'Ram', 'Sham', 'Ramesh')
Employee ID (23, 1, 4, 21) 
Employee Salary (60000, 40000, 50000, 70000)

Explanation

In the above example, unzipping is done with the help of ‘*’ operator. Unzipping means converting the zipped values to their older version i.e a zipped object will be converted into list if it was a list before. 


×