How to Use enumerate() in Python

Lesson 1 of 6 · ·

When you loop over a list in Python you usually want the item, and sometimes you also need to know where it sits: its position, a line number, a step number for a report. Python enumerate() gives you both in one line, so you never have to keep a counter by hand. This lesson shows how it works, the two options worth knowing, and a worked example you can reuse.

Playing loads the video from YouTube. Watch on YouTube

The problem enumerate() solves

Here is the version many people write first. It works, but the counter is extra code you have to remember to update, and forgetting the last line gives every item the same number.

tasks = ["Back up the database", "Renew the SSL certificate", "Update the README"]

index = 0
for task in tasks:
    print(index, task)
    index += 1

With enumerate() the counter comes with the loop. Each time round, it hands you a pair: the position and the item. You unpack the pair into two names in the for line.

for index, task in enumerate(tasks):
    print(index, task)
0 Back up the database
1 Renew the SSL certificate
2 Update the README

What enumerate() actually returns

enumerate() doesn’t build a new list. It returns a small iterator that produces (index, item) pairs on demand, which is why it costs almost nothing on a long list. If you want to see the pairs, turn it into a list:

print(list(enumerate(["a", "b", "c"])))
[(0, 'a'), (1, 'b'), (2, 'c')]

It works on anything you can loop over: lists, tuples, strings, the lines of a file, the keys of a dictionary.

Counting from 1 with start

Positions start at 0 because that’s how Python indexes lists. People count from 1. When the number is for a person to read, pass start=1:

for number, task in enumerate(tasks, start=1):
    print(f"{number}. {task}")
1. Back up the database
2. Renew the SSL certificate
3. Update the README

start only changes the number you get; it doesn’t skip any items.

Worked example: report the bad rows in a file with their line numbers

A common real task: check the rows of a CSV export and report the ones that fail, with line numbers someone can find in the file. The header is line 1, so the data starts on line 2. Slicing off the header and starting the count at 2 gives the right line numbers with no arithmetic.

lines = [
    "date,product,units",
    "2026-10-01,Cable,12",
    "2026-10-01,Charger,",
    "2026-10-02,Case,7",
    "2026-10-02,Cable,x",
]

problems = []
for line_number, line in enumerate(lines[1:], start=2):
    date, product, units = line.split(",")
    if not units.isdigit():
        problems.append((line_number, product, units or "(empty)"))

for line_number, product, value in problems:
    print(f"Line {line_number}: {product} has units {value!r}")
Line 3: Charger has units '(empty)'
Line 5: Cable has units 'x'

With a real file you would loop over the open file object instead of a list, and enumerate() works the same way.

enumerate() with zip(): two lists, position by position

zip() walks two lists side by side; wrapping it in enumerate() adds the position. Note the brackets around the zipped pair in the for line.

forecast = [120, 95, 140]
actual = [118, 101, 150]

for week, (f, a) in enumerate(zip(forecast, actual), start=1):
    print(f"Week {week}: forecast {f}, actual {a}, off by {abs(f - a)}")
Week 1: forecast 120, actual 118, off by 2
Week 2: forecast 95, actual 101, off by 6
Week 3: forecast 140, actual 150, off by 10

Changing a list while you loop over it

Assigning to the loop variable doesn’t change the list. To update items in place you need the index, and enumerate() gives it to you:

prices = [10.0, 12.5, 8.0]
for i, price in enumerate(prices):
    prices[i] = round(price * 1.1, 2)
print(prices)
[11.0, 13.75, 8.8]

Strings, dictionaries and sorted results

Because enumerate() takes anything you can loop over, it numbers the characters of a string just as easily:

for position, letter in enumerate("SKU"):
    print(position, letter)
0 S
1 K
2 U

Looping over a dictionary gives you its keys; loop over .items() to get key and value pairs. Combined with sorted(), this is a quick way to print a ranked list, here products by units in stock:

stock = {"cable": 12, "case": 0, "charger": 5}
ranked = sorted(stock.items(), key=lambda kv: kv[1], reverse=True)
for rank, (item, units) in enumerate(ranked, start=1):
    print(f"{rank}. {item}: {units}")
1. cable: 12
2. charger: 5
3. case: 0

Try it yourself

Given scores = [72, 88, 95, 61, 88], find the position of every score equal to the highest one. Try it before reading the answer: a list comprehension with enumerate() does it in one line.

scores = [72, 88, 95, 61, 88]
best = max(scores)
print([i for i, s in enumerate(scores) if s == best])
[2]

Change one of the 88s to 95 and run it again: you get both positions, which is exactly why the answer is a list.

Mistakes to avoid

  • Writing for i in range(len(items)) and then items[i]: it works, but enumerate() is shorter and harder to get wrong.
  • Forgetting to unpack: for pair in enumerate(items) gives you tuples, not two separate names.
  • Using start to skip items: it only changes the numbers. To skip items, slice the list first, as in the worked example.

Next in the series: handling errors with try and except, so one bad row doesn’t stop the whole program.

Want to go further?

I teach professionals one to one, and I build these systems for businesses. See Python AI mentoring or see what I build.