Python enumerate() Function



The python 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.

The counter serve as a 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.

In the next few sections, we will be learning more about this function.

Syntax

The following is the syntax for the python enumerate() function.

enumerate(iterable, startIndex)

Parameters

The following is the parameter of the python enumerate() function −

  • iterable − This parameter indicates an iterable object such as a list, tuple, dictionary, or set.

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

Return Value

The python enumerate() function returns enumerated object.

Examples

Now, let's understand the working of enumerate() function with the help of some examples −

Example 1

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 2

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 3

The enumerate() function can also be combined with the for 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: Grapes
Index: 1, Value: Apple
Index: 2, Value: Banana
Index: 3, Value: Kiwi

Example 4

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: Grapes
Index: 1, Quantity: 10, Fruit: Apple
Index: 2, Quantity: 9, Fruit: Banana
Index: 3, Quantity: 12, Fruit: Kiwi
python_built_in_functions.htm
Advertisements