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=1if you want the index to begin at 1 instead of 0.
Here is an example using start=1:
123cities = ['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 withstart;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:
12345travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(str(index) + ' ' + city)
You can use str() to convert a number into text before combining it with a string.
Thanks for your feedback!
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=1if you want the index to begin at 1 instead of 0.
Here is an example using start=1:
123cities = ['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 withstart;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:
12345travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Printing all cities with their indexes for index, city in enumerate(travel_list): print(str(index) + ' ' + city)
You can use str() to convert a number into text before combining it with a string.
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
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat