How to Use enumerate() in Python
enumerate() helps you loop over items and get both the index and the value at the same time.
This is useful when you want to:
- print numbered items
- track where something appears in a list
- build simple menus
- avoid managing a separate counter variable
If you are still getting comfortable with loops, this function can make your code cleaner and easier to read. If you need a refresher first, see Python for loops explained.
Quick answer
fruits = ["apple", "banana", "orange"]
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple
1 banana
2 orange
Use enumerate() when you need the position and the item together in a loop.
What enumerate() does
enumerate() adds a counter to an iterable.
That means it can take something like a list or string and give you:
- the index
- the current value
It is commonly used in for loops because it saves you from creating and updating a separate counter yourself.
It works with many iterables, including:
- lists
- tuples
- strings
- other iterable objects
If you want a deeper definition of the function itself, see the Python enumerate() function reference.
Basic syntax
The most common pattern looks like this:
for index, value in enumerate(items):
print(index, value)
Here is what each part means:
itemsis the iterable you want to loop overindexis the position numbervalueis the current item
By default, counting starts at 0.
Simple example with a list
Here is a basic example using a list of fruits:
fruits = ["apple", "banana", "orange"]
for index, fruit in enumerate(fruits):
print(index, fruit)
Output:
0 apple
1 banana
2 orange
What is happening here:
- On the first loop,
indexis0andfruitis"apple" - On the second loop,
indexis1andfruitis"banana" - On the third loop,
indexis2andfruitis"orange"
This is often clearer than using range(len(fruits)), especially for beginners.
If you want more practice with list loops, see how to loop through a list in Python.
How to start counting from 1
Sometimes you want numbering to start at 1 instead of 0.
You can do that with the start argument:
fruits = ["apple", "banana", "orange"]
for number, fruit in enumerate(fruits, start=1):
print(number, fruit)
Output:
1 apple
2 banana
3 orange
This is useful for:
- numbered menus
- user-facing lists
- ranked output
Important: this only changes the number that enumerate() gives you in the loop. It does not change the real index positions in the list.
For example:
fruits = ["apple", "banana", "orange"]
for number, fruit in enumerate(fruits, start=1):
print(number, fruit)
print(fruits[0])
Output:
1 apple
2 banana
3 orange
apple
Even though the display starts at 1, the first list item is still at index 0.
Common beginner use cases
Printing a numbered list
tasks = ["wash dishes", "study Python", "go for a walk"]
for number, task in enumerate(tasks, start=1):
print(f"{number}. {task}")
Output:
1. wash dishes
2. study Python
3. go for a walk
Tracking item positions while looping
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(f"{color} is at position {index}")
Output:
red is at position 0
green is at position 1
blue is at position 2
Finding where a match appears
names = ["Ana", "Ben", "Cara", "Ben"]
for index, name in enumerate(names):
if name == "Ben":
print(f"Found Ben at index {index}")
Output:
Found Ben at index 1
Found Ben at index 3
Building a simple menu from a list
options = ["Start game", "Settings", "Quit"]
for number, option in enumerate(options, start=1):
print(f"{number}. {option}")
Output:
1. Start game
2. Settings
3. Quit
Why enumerate() is better than manual counters
Before learning enumerate(), beginners often write code like this:
fruits = ["apple", "banana", "orange"]
index = 0
for fruit in fruits:
print(index, fruit)
index += 1
This works, but enumerate() is usually better:
fruits = ["apple", "banana", "orange"]
for index, fruit in enumerate(fruits):
print(index, fruit)
Why this is better:
- less code to write
- easier to read
- lower chance of off-by-one mistakes
- no risk of forgetting to update the counter
If you often write range(len(items)) just to get both the index and the value, enumerate() is usually the clearer choice.
Common mistakes
Forgetting to unpack two values
enumerate() gives you two things each time through the loop:
- the index
- the value
So this is correct:
letters = ["a", "b", "c"]
for index, letter in enumerate(letters):
print(index, letter)
But if you expect only one value, your loop will not work the way you expect.
Mixing up index and item order
The order is:
- index
- value
This is correct:
letters = ["a", "b", "c"]
for index, letter in enumerate(letters):
print(index, letter)
Not this:
letters = ["a", "b", "c"]
for letter, index in enumerate(letters):
print(letter, index)
That second example runs, but the variable names are misleading because the first value is still the index.
Assuming start=1 changes real list indexing
This is a very common mistake.
items = ["a", "b", "c"]
for number, item in enumerate(items, start=1):
print(number, item)
print(items[1])
Output:
1 a
2 b
3 c
b
The list still uses normal zero-based indexing.
Using enumerate() when only the values are needed
If you only need each item, loop directly over the iterable:
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
That is simpler than using enumerate() when you do not need the index.
If you get unpacking errors while trying examples like these, see how to fix ValueError: too many values to unpack.
When not to use enumerate()
enumerate() is helpful, but not always the best tool.
Do not use it when:
- you only need each item and not the position
- you need a custom step pattern that would be clearer another way
- you need to loop over multiple iterables together
If you need to combine items from two lists at the same time, zip() in Python is often a better fit.
FAQ
What does enumerate() return in Python?
It returns an enumerate object that produces pairs of index and value while you loop.
You can see that by converting it to a list:
print(list(enumerate(["a", "b", "c"])))
Output:
[(0, 'a'), (1, 'b'), (2, 'c')]
Does enumerate() start at 0 or 1?
It starts at 0 by default, but you can change that with start=1 or any other number.
Example:
print(list(enumerate(["a", "b", "c"], start=1)))
Output:
[(1, 'a'), (2, 'b'), (3, 'c')]
Can I use enumerate() with strings?
Yes. It works with strings and gives the position and each character.
word = "cat"
for index, char in enumerate(word):
print(index, char)
Output:
0 c
1 a
2 t
Is enumerate() better than range(len(list))?
For many beginner cases, yes.
If you need both the index and the value, enumerate() is usually clearer and easier to read.