Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python enumerate() Function



ThePython enumerate() function is used to access each item from an iterable object. This function accepts an iterable object and returns it as an enumerate object. It also adds a counter to each iterable item to streamline the process of iteration.

Thecounter serves as an index to each item of the iterable which helps in traversing. Remember, an iterable is an object that enables us to access its items by iterating over them.

Theenumerate() function is one of thebuilt-in functions and does not require any module to import.

Syntax

The following is the syntax for the pythonenumerate() function.

enumerate(iterable, startIndex)

Parameters

The following is the parameter of the pythonenumerate() function −

  • iterable − This parameter indicates an iterable object such as alist,tuple,dictionary, orset.

  • startIndex − It specifies the starting index of iterable object. It is an optional parameter whose default value is 0.

Return Value

The pythonenumerate() function returns enumerated object.

enumerate() Function Examples

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

Example: Use of enumerate() Function

The following is an example of the Python enumerate() function. In this, we have created a list and, trying to access its each element using the enumerate() function.

fruitLst = ["Grapes", "Apple", "Banana", "Kiwi"]newLst = list(enumerate(fruitLst))print("The newly created list:")print(newLst)

On executing the above program, the following output is generated −

The newly created list:[(0, 'Grapes'), (1, 'Apple'), (2, 'Banana'), (3, 'Kiwi')]

Example: enumerate() Function With 'start' Parameter

If we specify the starting index for the enumeration, the index number will start from the specified value of index. In the code below, we are specifying 1 as the start index.

fruitLst = ["Grapes", "Apple", "Banana", "Kiwi"]newLst = list(enumerate(fruitLst, start=1))print("The newly created list:")print(newLst)

The following is the output obtained by executing the above program −

The newly created list:[(1, 'Grapes'), (2, 'Apple'), (3, 'Banana'), (4, 'Kiwi')]

Example: enumerate() Function With for Loop

The enumerate() function can also be combined with thefor loop to access the element in a list as demonstrated in the following example.

fruitLst = ["Grapes", "Apple", "Banana", "Kiwi"]print("The newly created list:")for index, fruit in enumerate(fruitLst):   print(f"Index: {index}, Value: {fruit}")

The following output is obtained by executing the above program −

The newly created list:Index: 0, Value: GrapesIndex: 1, Value: AppleIndex: 2, Value: BananaIndex: 3, Value: Kiwi

Example: enumerate() Function With List of Tuples

In the below example, we are creating a list of tuples and applying the enumerate() function to display all the elements of the list.

fruits = [(8, "Grapes"), (10, "Apple"), (9, "Banana"), (12, "Kiwi")]for index, (quantity, fruit) in enumerate(fruits):   print(f"Index: {index}, Quantity: {quantity}, Fruit: {fruit}")

The above program, on executing, displays the following output -

Index: 0, Quantity: 8, Fruit: GrapesIndex: 1, Quantity: 10, Fruit: AppleIndex: 2, Quantity: 9, Fruit: BananaIndex: 3, Quantity: 12, Fruit: Kiwi
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp