Online Tutorials & Training Materials | STechies.com
Register Login

Zip() Function in Python with Explained Examples

18 Sep 2019 12:39 pm || 0

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.

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)}

Example with for loop

# 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

Example 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

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)} Employee Name: ('Harish', 'Ram', 'Sham', 'Ramesh') Employee ID (23, 1, 4, 21) Employee Salary (60000, 40000, 50000, 70000)