Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python zip() Function



ThePython zip() function is abuilt-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 Pythonzip() function −

zip(iterator...)

Parameters

The Pythonzip() function accepts a single parameter −

  • iterator − It represents an object such as alist, ortuple. This parameter can accept any number of iterator object.

Return Value

The Pythonzip() function returns an iterator object.

zip() Function Examples

Practice the following examples to understand the use ofzip() function in Python:

Example: Use of zip() Function

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: Create Dictionary Using zip() Function

Apart from tuple,zip() function can also be used to createdictionaries 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: Unzip Iterable Objects Using zip() Function

We can also unzip values using thezip() function. If we pass the asterisk sign "*" along with the iterable object to thezip() 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: 121Name: Shrey, ID: 131Name: Raman, ID: 141

Example: Iterate Multiple Lists Parallelly Using zip() Function

When we have multiple lists of the same length, thezip() 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: 121Shrey has ID no: 131Raman has ID no: 141
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp