Online Tutorials & Training Materials | STechies.com
Register Login

Zip() Function in Python with Explained Examples

|| 0

Zip() Function in Python with Explained Examples
Stechies

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 first item in each passed iterator is paired together and like wise. 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 lenght of the new iterator, Meaning if we passed two iterables, one iterable containing 5 items and second iterable containg 7 items, then the python zip() function will return iterator containing 5 tuples.

Syntax

zip(iter1, iter2, iter3 ...)

Parameters:

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

Return Value:

Returns a single iterator object

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 obejct length will be decide on the basis of which iterator having less 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)