Python enumerate

Summary: in this tutorial, you will learn about the enumerate() function and how to use it to enumerate a sequence, iterator, or any other object that supports iteration.

Introduction to the Python enumerate() function #

The following example defines a list of fruits and uses the for-in loop to iterate over the list elements and print them out:

fruits = ['Apple', 'Orange', 'Strawberry']

for fruit in fruits:
    print(fruit)Code language: Python (python)

Output:

Apple
Orange    
StrawberryCode language: Python (python)

It works fine as expected.

Sometimes, you want to add indices to the output. To do that, you may come up with a loop counter like this:

fruits = ['Apple', 'Orange', 'Strawberry']

index = 1

for fruit in fruits:
    print(f'{index}.{fruit}')
    index += 1Code language: Python (python)

Output:

1.Apple
2.Orange
3.StrawberryCode language: Python (python)

In this example, the index keeps track of the indices. This example also works as expected. But Python has a better solution with the enumerate() function:

fruits = ['Apple', 'Orange', 'Strawberry']

for index, fruit in enumerate(fruits, start=1):
    print(f'{index}.{fruit}')Code language: Python (python)

Output:

1.Apple
2.Orange
3.StrawberryCode language: Python (python)

With the enumerate() function, you can achieve the same result with less code. Also, you don’t need to keep track of the index by increasing it in each iteration.

The enumerate() is a built-in function with the following syntax:

enumerate(iterable, start=0)Code language: Python (python)

The enumerate() function accepts two arguments:

  • iterable is a sequence, an iterator, or any object that supports the iterable protocol.
  • start is the starting number from which the function starts counting. It defaults to zero.

The enumerate() function returns an enumerate object. The __next__() method of the returned enumerate object returns a tuple that contains a count and the value obtained from iterating over the iterable.