Python enumerate() Function Explained
enumerate() is a built-in Python function that helps you loop through items while also keeping track of their position.
It is useful when you need both:
- the item itself
- its index in the loop
Beginners often use a manual counter variable for this, but enumerate() is usually cleaner and easier to read.
Quick example
colors = ["red", "green", "blue"]
for index, color in enumerate(colors):
print(index, color)
Output:
0 red
1 green
2 blue
Use enumerate() when you need both the item and its position in a loop.
What enumerate() does
enumerate() lets you loop over an iterable and get both the index and the value at the same time.
It is commonly used with:
- lists
- tuples
- strings
- other iterable objects
This helps you avoid writing and updating your own counter variable.
Example:
names = ["Ana", "Ben", "Cara"]
for index, name in enumerate(names):
print(index, name)
Output:
0 Ana
1 Ben
2 Cara
If you are still getting comfortable with loops, see Python for loops explained.
Basic syntax
The syntax is:
enumerate(iterable, start=0)
It has two parts:
iterable: the object you want to loop throughstart: the number to begin counting from
By default, counting starts at 0.
Example:
letters = ["a", "b", "c"]
for i, letter in enumerate(letters):
print(i, letter)
Output:
0 a
1 b
2 c
If you want to compare this with another counting tool, see the Python range() function explained.
What enumerate() returns
enumerate() returns an enumerate object.
In most cases, you use it directly in a for loop. Each step of the loop gives you a pair:
(index, item)
You usually unpack that pair into two variables.
Example:
fruits = ["apple", "banana", "orange"]
for index, fruit in enumerate(fruits):
print(index, fruit)
You can also convert the result to a list if you want to see all the pairs at once:
numbers = [10, 20, 30]
result = list(enumerate(numbers))
print(result)
Output:
[(0, 10), (1, 20), (2, 30)]
This is a good way to understand what enumerate() produces.
When to use enumerate()
Use enumerate() when you need the item number while looping.
Common cases:
- printing numbered output
- building menus
- showing positions in a list
- looping over both index and value together
Example:
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
If you need both the index and the value, enumerate() is usually easier to read than range(len(...)).
If you want more practical examples, see how to use enumerate() in Python.
Using the start argument
You can change where counting begins by passing the start argument.
For example, if you want counting to start at 1 instead of 0:
colors = ["red", "green", "blue"]
for index, color in enumerate(colors, start=1):
print(index, color)
Output:
1 red
2 green
3 blue
This is especially useful for user-facing output, such as menus or numbered lists.
The original iterable does not change. Only the counting value changes.
Common beginner confusion
Here are some common mistakes beginners make with enumerate().
The index is not the same as the value
In this loop:
numbers = [10, 20, 30]
for index, value in enumerate(numbers):
print(index, value)
indexis0,1,2valueis10,20,30
These are different things.
You must unpack into two variables
Each item from enumerate() is a pair.
This works:
items = ["x", "y"]
for i, item in enumerate(items):
print(i, item)
But this causes an error:
items = ["x", "y"]
for i, item, extra in enumerate(items):
print(i, item, extra)
Because enumerate() only gives two values each time: the index and the item.
enumerate() only works with iterable objects
You can use it with things like lists, strings, and tuples.
Example with a string:
for index, char in enumerate("cat"):
print(index, char)
Output:
0 c
1 a
2 t
To understand why this works, see iterators and iterable objects explained.
If you only need values, a normal loop is enough
You do not always need enumerate().
If you only need the items, use a normal for loop:
colors = ["red", "green", "blue"]
for color in colors:
print(color)
That is simpler when the index is not needed.
Common mistakes
Watch out for these common problems:
- Trying to unpack
enumerate()into the wrong number of variables - Forgetting that counting starts at
0by default - Using
enumerate()when only values are needed - Confusing
enumerate()withrange()
These quick checks can help you understand what is happening:
print(enumerate([10, 20, 30]))
print(list(enumerate([10, 20, 30])))
print(list(enumerate([10, 20, 30], start=1)))
for i, value in enumerate(['a', 'b']):
print(i, value)
Example output:
<enumerate object at 0x...>
[(0, 10), (1, 20), (2, 30)]
[(1, 10), (2, 20), (3, 30)]
0 a
1 b
FAQ
What does enumerate() return in Python?
It returns an enumerate object that produces pairs of index and value.
Does enumerate() start at 0 or 1?
It starts at 0 by default, but you can change that with start=1 or another number.
Should I use enumerate() instead of range(len(list))?
If you need both the index and the value, enumerate() is usually clearer and easier to read.
Can enumerate() be used with strings?
Yes. A string is iterable, so enumerate() can return each character with its index.