Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn The enumerate() Function | Section
Python Loops

The enumerate() Function

When looping through a sequence like a list, you often want both the item itself and its position in the list. A standard for loop, such as for city in travel_list:, gives you just the value—no information about its index. The enumerate() function solves this by yielding pairs of (index, value) tuples on each iteration:

  • index: the 0-based position of the item in the sequence;
  • value: the element at that position.

You can use two variables in the loop—for index, value in enumerate(sequence):—to unpack these pairs directly. This makes your code cleaner and avoids manually tracking the index with a counter variable.

You can also use the optional start parameter to set the starting index. For example, enumerate(sequence, start=1) will number the first item as 1 instead of 0. This is helpful when you want numbering to match real-world lists or user-facing labels.

The syntax for using enumerate() is:

for index, value in enumerate(sequence):
    # Your code here

You can also specify a starting value for the index using the optional start parameter:

for index, value in enumerate(sequence, start=0):
    # Your code here
  • By default, start=0, so indexing begins at 0;
  • You can set start=1 if you want the index to begin at 1 instead of 0.

Here is an example using start=1:

123
cities = ['Monako', 'Luxemburg', 'Liverpool'] for index, city in enumerate(cities, start=1): print(f"City {index}: {city}")
  • index: refers to the position of an element in the list, starting from the value you set with start;
  • value: refers to the actual element at a given index.

Let's apply enumerate() to our travel_list to print each city along with its index:

12345
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(str(index) + ' ' + city)
Note
Note

You can use str() to convert a number into text before combining it with a string.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 6
single

single

The enumerate() Function

Swipe to show menu

When looping through a sequence like a list, you often want both the item itself and its position in the list. A standard for loop, such as for city in travel_list:, gives you just the value—no information about its index. The enumerate() function solves this by yielding pairs of (index, value) tuples on each iteration:

  • index: the 0-based position of the item in the sequence;
  • value: the element at that position.

You can use two variables in the loop—for index, value in enumerate(sequence):—to unpack these pairs directly. This makes your code cleaner and avoids manually tracking the index with a counter variable.

You can also use the optional start parameter to set the starting index. For example, enumerate(sequence, start=1) will number the first item as 1 instead of 0. This is helpful when you want numbering to match real-world lists or user-facing labels.

The syntax for using enumerate() is:

for index, value in enumerate(sequence):
    # Your code here

You can also specify a starting value for the index using the optional start parameter:

for index, value in enumerate(sequence, start=0):
    # Your code here
  • By default, start=0, so indexing begins at 0;
  • You can set start=1 if you want the index to begin at 1 instead of 0.

Here is an example using start=1:

123
cities = ['Monako', 'Luxemburg', 'Liverpool'] for index, city in enumerate(cities, start=1): print(f"City {index}: {city}")
  • index: refers to the position of an element in the list, starting from the value you set with start;
  • value: refers to the actual element at a given index.

Let's apply enumerate() to our travel_list to print each city along with its index:

12345
travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(str(index) + ' ' + city)
Note
Note

You can use str() to convert a number into text before combining it with a string.

Task

Swipe to start coding

You're organizing your travel wishlist and want to add a short label to each country that includes its position in the list.

  • Use the enumerate() function to loop through the list of countries.
  • For each country, create a string that says something like: "Destination 1: France".
  • Store these strings in a new list called travel_rankings.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt