Python enumerate() Function
enumerate(iterable, start=0) wraps an iterable so a for loop receives (index, item) pairs, counting from start.Whenever you need the position of an item while looping, enumerate() is the clean answer. It replaces the old habit of writing for i in range(len(items)) and then indexing, which is longer and easier to get wrong.
The counter works with any iterable, not only lists: strings, tuples, files and generators. You usually unpack each pair directly in the for loop header.
| Syntax | enumerate(iterable, start=0) |
|---|---|
| Parameters | iterable: anything you can loop over; start: first number to count from (default 0) |
| Returns | An enumerate iterator that yields (count, item) tuples |
| Changes the original? | No |
| Time complexity | O(1) per item, O(n) for a full loop |
How enumerate() counts items
Each time the loop asks for the next value, enumerate() takes the next item from your iterable and pairs it with a counter. The counter has nothing to do with the item's real index: it simply starts at start and goes up by one.
letters = ["a", "b", "c"]
print(list(enumerate(letters)))
print(list(enumerate(letters, start=1)))Because the result is an iterator, it is consumed once. Convert it with list() if you need to reuse it.
Changing the starting number with start
Humans count from 1, so start=1 is ideal for numbered menus, rankings and line numbers. The start argument can be passed positionally (enumerate(items, 1)) or by name. Writing start=1 is clearer to read.
Note that start only changes the label. It does not skip items: to skip items use slicing such as items[1:].
enumerate() vs range(len()) vs zip()
| Approach | Gives you | When to use it |
|---|---|---|
enumerate(items) | Index and item together | Default choice when you need both |
range(len(items)) | Only the index | Rarely; when you modify items[i] by position |
zip(range(n), items) | Index and item | Not needed; enumerate does this directly |
zip(a, b) | Items from two iterables | Walking two lists in parallel, see zip() |
Examples you can run
Press Run, then edit the code and run it again.
Number the items in a shopping list
Start at 1 so the numbers match how people count.
cart = ["pen", "notebook", "stapler"]
for number, item in enumerate(cart, start=1):
print(number, item)Expected output1 pen
2 notebook
3 stapler
Find the positions of matching items
Use the counter as a position and keep only the ones you want.
scores = [40, 91, 55, 91, 70]
best = [i for i, s in enumerate(scores) if s == 91]
print(best)Expected output[1, 3]
Build a dictionary from a list
dict(enumerate(...)) maps each position to its item.
days = ["Mon", "Tue", "Wed"]
print(dict(enumerate(days)))Expected output{0: 'Mon', 1: 'Tue', 2: 'Wed'}
Update a list by position
When you assign back into the list, you need the index, and enumerate() supplies it.
prices = [10, 20, 30]
for i, p in enumerate(prices):
prices[i] = p * 2
print(prices)Expected output[20, 40, 60]
Common mistakes
Unpacking in the wrong order
✗ Wrong
names = ["Asha", "Ravi"]
for name, i in enumerate(names):
print(i, name)✓ Right
names = ["Asha", "Ravi"]
for i, name in enumerate(names):
print(i, name)enumerate() always yields (index, item). Put the index first.
Passing a number instead of an iterable
✗ Wrong
for i, x in enumerate(5):
print(i, x)✓ Right
for i, x in enumerate(range(5)):
print(i, x)Give enumerate() something you can loop over. To count from 0 to 4 use range(5).
Expecting start to skip items
✗ Wrong
items = ["a", "b", "c"]
for i, x in enumerate(items, start=1):
print(i, x)✓ Right
items = ["a", "b", "c"]
for i, x in enumerate(items[1:], start=1):
print(i, x)Slice the list if you want to skip items: items[1:].
Python enumerate() Function: questions people ask
What does enumerate() do in Python?
It adds a counter to an iterable and yields (index, item) pairs. This lets a for loop know the position of each item without manual counting.
How do I start enumerate at 1?
Pass start=1, as in enumerate(items, start=1). The first pair is then (1, first_item).
Is enumerate() better than range(len())?
Yes in most cases. enumerate() is shorter, more readable and works with iterables that have no length, such as generators.
Keep learning
Step-by-step lessons:
Python enumerate() and zip()Python for LoopsPython Lists
Related:
Python zip() FunctionPython range() FunctionPython reversed() FunctionPython len() Function
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →