Python zip() Function



The Python zip() function is a built-in function that allows the aggregation of elements from two or more iterables, such as lists, tuples, etc. It creats a new iterable of tuples where each tuple contains the items from the original iterables that are located at the same index.

If we pass iterables of two different lengths, then, the iterable with the least number of items will decide the length of the new iterator.

Syntax

Following is the syntax of the Python zip() function −

zip(iterator...)

Parameters

The Python zip() function accepts a single parameter −

  • iterator − It represents an object such as a list, or tuple. This parameter can accept any number of iterator object.

Return Value

The Python zip() function returns an iterator object.

Examples

In this section, we will see some examples of zip() function −

Example 1

The most common use of zip() function is to combine elements from two or more iterables into a list of tuples. In the following example, we are combining two lists containing fruits and their quantity details.

fruits = ["grapes", "orange", "kiwi", "apple", "mango"]
quantity = [25, 30, 35, 20, 15]
newList = list(zip(fruits, quantity))
print("The new list containing both lists:")
print(newList)

When we run above program, it produces following result −

The new list containing both lists:
[('grapes', 25), ('orange', 30), ('kiwi', 35), ('apple', 20), ('mango', 15)]

Example 2

Apart from tuple, zip() function can also be used to create dictionaries where the keys and values are provided by two separate lists. In the code below, we are illustrating the same.

keyOfDict = ["empId", "designation", "department"]
valOfDict = [121, "developer", "Technology"]
empDetails = dict(zip(keyOfDict, valOfDict))
print("The new dictionary containing both lists:")
print(empDetails)

Following is an output of the above code −

The new dictionary containing both lists:
{'empId': 121, 'designation': 'developer', 'department': 'Technology'}

Example 3

We can also unzip values using the zip() function. If we pass the asterisk sign "*" along with the iterable object to the zip() function, it will separate the elements of that iterable. In the below exmple, we are unzipping the list named "empDetails".

empDetails = [("Ansh", 121), ("Shrey", 131), ("Raman", 141)]
names, ids = zip(*empDetails)
print("Displaying the details:")
for emp, empId in zip(names, ids):
    print(f"Name: {emp}, ID: {empId}")

Output of the above code is as follows −

Displaying the details:
Name: Ansh, ID: 121
Name: Shrey, ID: 131
Name: Raman, ID: 141

Example 4

When we have multiple lists of the same length, the zip() function allows us to iterate over them in parallel as illustrated in the code below.

empDetails = [("Ansh", 121), ("Shrey", 131), ("Raman", 141)]
names, ids = zip(*empDetails)
print("Displaying the details:")
for emp, empId in zip(names, ids):
    print(f"{emp} has ID no: {empId}")

Following is an output of the above code −

Displaying the details:
Ansh has ID no: 121
Shrey has ID no: 131
Raman has ID no: 141
python_built_in_functions.htm
Advertisements