`enumerate()`: Index + Value in One Go
The enumerate() function lets you loop over items and their indexes at the same time. It’s one of the most Pythonic tools for writing clear, readable loops without relying on range(len()).
If you’ve written Python for a while, you’ve probably seen (or written) loops using range(len(...)).
They work — but they’re rarely the most readable option.
That’s exactly the problem enumerate() solves. It allows you to iterate over both the index and the value, in a way that’s clearer, safer, and more idiomatic.
When enumerate() really shines
enumerate() is ideal when:
- You need both the index and the value
- You want to avoid manual index handling
- You care about readability and intent
Instead of thinking about positions, you focus on the data itself.
A common pattern without enumerate()
items = ["apple", "banana", "orange"]
for i in range(len(items)):
print(i, items[i])
This works, but it’s verbose and easier to get wrong if the code grows.
The same loop with enumerate()
items = ["apple", "banana", "orange"]
for index, value in enumerate(items):
print(index, value)
Now the intent is explicit:
indexis the positionvalueis the element
No extra indexing, no mental overhead.
Starting the index at a custom value
A feature many people forget is the start parameter:
items = ["apple", "banana", "orange"]
for position, value in enumerate(items, start=1):
print(position, value)
This is especially useful for user-facing lists, reports, or logs.
A common mistake to avoid
Sometimes developers reach for enumerate when they don’t actually need the index:
# Unnecessary enumerate
for i, value in enumerate(items):
print(value)
If you don’t use i, a simple loop is clearer:
for value in items:
print(value)
Conclusion
In my experience, enumerate() is one of those small Python tools that quietly improves code quality.
Whenever you find yourself reaching for range(len(...)), it’s worth asking:
Would enumerate() make this clearer?
Most of the time, the answer is yes.