How to Use Enumerate in Python

What you’ll build or solve

You’ll loop over items and their indexes at the same time using enumerate().

When this approach works best

enumerate() works well when you:

  • Print numbered results, like “1. item”, “2. item”, and so on.
  • Need both the index and the value, like validating rows from a CSV-like list.
  • Modify a list in place by index while still reading each value.

Avoid enumerate() when you only need the values. A plain for item in items: loop is simpler.

Prerequisites

  • Python installed
  • You know how a for loop works

Step-by-step instructions

1) Loop with index and value

enumerate() returns pairs of (index, value) as you loop.

names= ["Naomi","Ivan","Lea"]

fori,nameinenumerate(names):
print(i,name)

What to look for:

The index starts at 0 by default, which matches Python list indexing. enumerate() works with any iterable, not just lists.


2) Start counting from 1 (or any number)

Use the start= argument when you want human-friendly numbering.

tasks= ["Email client","Fix bug","Deploy"]

forn,taskinenumerate(tasks,start=1):
print(f"{n}.{task}")

What to look for:

start=1 affects only the numbers you get from enumerate(). It does not change how list indexes work.


3) Update a list in place using the index

If you need to change items, use the index from enumerate().

tags= ["  python ","  SQL"," css  "]

fori,taginenumerate(tags):
tags[i]=tag.strip().lower()

print(tags)

What to look for:

Updating tags[i] changes the original list. This is different from building a new list.


Examples you can copy

Example 1: Number menu options

options= ["Play","Settings","Quit"]

forn,optioninenumerate(options,start=1):
print(f"{n}){option}")

Example 2: Find the index of the first match

names= ["mila","ana","ivan"]
target="ana"

found_at=None

fori,nameinenumerate(names):
ifname==target:
found_at=i
break

print(found_at)

Example 3: Collect errors with line numbers

rows= ["10","x","30",""]

errors= []

forline_no,textinenumerate(rows,start=1):
ifnottext.isdigit():
errors.append((line_no,text))

print(errors)

Example 4: Replace items based on position

items= ["a","b","c","d"]

fori,iteminenumerate(items):
ifi%2==0:
items[i]=item.upper()

print(items)

Example 5: Build a dictionary of indexes

names= ["mina","ana","ivan"]
name_to_index= {name:ifori,nameinenumerate(names)}
print(name_to_index)

Common mistakes and how to fix them

Mistake 1: Using the loop index as if it starts at 1

What you might do

names= ["Naomi","Ivan","Lea"]

fori,nameinenumerate(names):
print(f"{i}.{name}")

Why it breaks

The first printed number is 0, which looks wrong for user-facing output.

Fix

Start from 1:

names= ["Naomi","Ivan","Lea"]

fori,nameinenumerate(names,start=1):
print(f"{i}.{name}")

Mistake 2: Forgetting to unpack the pair

What you might do

names= ["Naomi","Ivan"]

forpairinenumerate(names):
print(pair)

Why it breaks

You get tuples like (0, "Naomi") instead of separate i and name variables.

Fix

Unpack in the loop header:

names= ["Naomi","Ivan"]

fori,nameinenumerate(names):
print(i,name)

Troubleshooting

If your numbering starts at 0, pass start=1.

If you see tuples like (0, 'Naomi'), unpack with for i, value in enumerate(...):.

If your updates do not stick, confirm you are writing back to the list using the index (items[i] = ...).

If you get “too many values to unpack,” the iterable items might already be pairs. Adjust your loop variables accordingly.

If enumerate() seems to stop early or produce no results, you may have passed an already-consumed iterator. Convert it to a list first if needed.


Quick recap

  • Use enumerate(items) to loop over indexes and values together.
  • Pass start=1 for human-friendly numbering.
  • Use the index from enumerate() to update list items in place.
  • enumerate() works with any iterable, not just lists.
  • Use a plain for loop when you only need values.