What Enumerate Does in Python, Explained Gently
Imagine you have a shopping list. You pick it up and count as you go. “One for milk, two for eggs, three for bread…” You do not need a separate pencil. The counting happens naturally.
Python has a similar trick called enumerate. It counts things for you while you look at them. No extra variables needed.
By the end of this short post, you will understand what enumerate does. You will use it in your own code. You will see why experienced Python writers prefer it.
How enumerate Works
A list in Python is just a row of items. When you loop over a list, Python hands you each item one by one. But sometimes you also want to know which item you’re looking at — its position, or index.
The old way looks like this:
fruits = ["apple", "banana", "cherry"]
i = 0
for fruit in fruits:
print(i, fruit)
i = i + 1
# prints: 0 apple / 1 banana / 2 cherry
It works. But notice that extra variable i. You create it, use it, and remember to bump it forward every loop. It’s easy to forget. It’s easy to mess up.
enumerate does the same thing, but in a cleaner way:
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, fruit)
# prints: 0 apple / 1 banana / 2 cherry
That’s it. One line of setup, one line of looping. The i and fruit appear together because they travel together. enumerate hands you a pair: the count, and the item.
This pattern is so common in Python that it has a nickname. People call it the Pythonic way to loop. You can read more about this style in the official Python tutorial on for statements.
Another helpful resource is Zero to Mastery’s guide to enumerate, which walks through the confusion many beginners experience.
Starting the Count Somewhere Else
By default, enumerate starts counting at zero. That matches how Python does everything — positions start at zero, not one. But sometimes you want a different starting number. Maybe you’re numbering steps in a recipe and you want to start at one.
There’s an optional second argument:
steps = ["boil water", "add pasta", "drain", "add sauce"]
for number, step in enumerate(steps, start=1):
print(f"Step {number}: {step}")
# prints: Step 1: boil water
# Step 2: add pasta
# Step 3: drain
# Step 4: add sauce
Pass start=1 and suddenly your counting line matches how humans count. No extra math.
Things That Might Confuse You
Using enumerate on a dictionary
Dictionaries do not behave exactly like lists. If you try to unpack two values from enumerate on a plain dictionary, you will not get keys and values. You will only get keys. That might surprise you.
scores = {"alice": 95, "bob": 82}
for i, item in enumerate(scores):
print(i, item)
# output: 0 alice 1 bob
This prints index and key only. The values are missing. That’s because looping over a dictionary gives you its keys, not its key-value pairs.
If you want both, use .items() instead of enumerate:
scores = {"alice": 95, "bob": 82}
for name, score in scores.items():
print(name, score)
# prints: alice 95 / bob 82
Use enumerate when you need a running count. Use .items() when you need pairs from a dictionary. They do different jobs.
Forgetting that counting starts at zero
Many beginners check the last item using len() directly.
colors = ["red", "green", "blue"]
print(colors[3])
This produces an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
The list has three items, but the highest index is 2, not 3. Python counts from zero, so the last item lives at index len(colors) - 1.
enumerate avoids this trap entirely because it just walks through the list. You never have to calculate indices yourself. The Python documentation on lists covers this indexing detail if you want a deeper look.
You’ve Got This
enumerate is a small built-in function that saves you from writing a manual counter. When you see a list, ask yourself one question. Do you need both the position and the item at the same time? If so, reach for enumerate. Try it on a list of your own — maybe a playlist or a todo list — and see how clean it looks. You’ll use it more than you think.
No comments yet. Be the first to leave a comment!