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)

Output:

Apple
Orange    
Strawberry

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 += 1

Output:

1.Apple
2.Orange
3.Strawberry

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}')

Output:

1.Apple
2.Orange
3.Strawberry

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)

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.

Python enumerate

See the following example:

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

e = enumerate(fruits, start=1)
print(type(e))

In this example, the enumerate() function returns e which is an instance of the enumerate class:

<class 'enumerate'>

The enumerate class has the __next__() method that returns a tuple:

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

e = enumerate(fruits, start=1)

print(e.__next__())

Output:

(1, 'Apple')

If you keep calling the __next__() method, it’ll return the next counting number with the value from the list:

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

e = enumerate(fruits, start=1)

print(e.__next__())
print(e.__next__())
print(e.__next__())

Output:

(1, 'Apple')
(2, 'Orange')
(3, 'Strawberry')

When all items in the list are iterated, calling the __next__() method will raise a StopIteration exception:

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

e = enumerate(fruits, start=1)

print(e.__next__())
print(e.__next__())
print(e.__next__())

print(e.__next__())  # StopIteration

The enumerate() is functionally equivalent to the following function:

def enumerate(iterable, start=0):
    index = start
    for item in iterable:
        yield index, item
        index += 1

Summary #

  • Use Python enumerate() function to get items of an iterable with a counter.

Was this helpful?