Free preview

100 lessons

Python Done Right

Free preview

Python Done Right · 100 lessons

Learn Python from scratch - syntax through to OOP

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

The enumerate() function

The enumerate() function

Explanation

The enumerate() function lets us iterate over a sequence while keeping track of each element's index position. It is particularly useful when we need both the item and its index in a loop.

Syntax

enumerate(iterable, start=0)
  • iterable - the sequence to iterate over (a list, tuple, string, etc.)
  • start - the starting value for the counter (defaults to 0)

enumerate() returns pairs of (index, element) as tuples. In a for loop, we unpack these directly:

for i, element in enumerate(iterable):
    # do something with the index, i, and the element
  • i, element unpacks the tuple returned by enumerate() into two variables that update on every iteration.
  • i counts up from 0 by 1 each iteration. We can name this anything, but i or index are common choices.
  • element holds the current value from the iterable. Again, we can name this anything we like.
  • iterable is the object we are iterating over, e.g. a list.

Example

Here we use enumerate() to loop over a list of characters, printing both the index and the value at each iteration:

characters = ['a', 'b', 'c', 'd']

for index, value in enumerate(characters):
    print(f"Index: {index}, Value: {value}")
Output

Details

The index starts at 0 and increments by 1 on each iteration, while value holds the corresponding element from the list.

Practice questions

4 questions

In the Python Editor you are given a list of numbers. Use the enumerate() function to loop over them. At each iteration, multiply the index from enumerate() by the number for the iteration.

What is value corresponding to the last iteration?

Select the correct answer:

+ 3 more questions

Specifying a starting value

Explanation

By default the counter starts at 0, but we can specify a different starting value by passing the start argument. This value can be any integer - useful when we want human-readable numbering or need to align indices with an external offset.

In this example, we iterate over a list of tasks and print the task number starting from 1 instead of 0. We also terminate the loop early using break:

tasks = ['Design', 'Develop', 'Test', 'Deploy']

# Enumerate starting from 1
for count, task in enumerate(tasks, start=1):
    print(f"Task {count}: {task}")
    if count == 3:
        print("--- Terminating after task 3 ---")
        break
Output

Details

Setting start=1 gives us a natural counting sequence. The break statement terminates the loop once count reaches 3, so 'Deploy' is never printed.

Info

We can also pass start as a positional argument - enumerate(tasks, 1) is equivalent to enumerate(tasks, start=1).

Practice questions

4 questions

In the Python Editor you are given a list of numbers. Use the enumerate() function to loop over them with a start value of -20. At each iteration, multiply the index from enumerate() by the number for the iteration.

What is value corresponding to the last iteration?

Select the correct answer:

+ 3 more questions