Loop with index and value
items = ["red", "green", "blue"]
for i, color in enumerate(items):
print(i, color)
# 0 red
# 1 green
# 2 blue
enumerate yields a pair on each loop — the index first, then the value — which you unpack into two variables.
Start counting from 1
items = ["first", "second", "third"]
for rank, item in enumerate(items, start=1):
print(rank, item)
# 1 first
# 2 second
# 3 third
The optional start argument sets where the count begins, which is handy for numbered lists shown to people.
Why not use range(len(...))?
items = ["a", "b", "c"]
# Clunky:
for i in range(len(items)):
print(i, items[i])
# Better:
for i, value in enumerate(items):
print(i, value)
The enumerate version is shorter, reads more clearly, and avoids indexing the list by hand.
Which approach should you use?
- enumerate — whenever you need both index and value.
- enumerate(items, start=1) — for human-friendly numbering.
- A plain for loop — when you do not need the index at all.
- Common mistake: writing
for i in enumerate(items)and forgetting to unpack the pair.
Frequently asked questions
Does enumerate work on strings and tuples?
Yes. enumerate works on any iterable, including strings, tuples, and file objects, not just lists.
How do I get a list of index and value pairs?
Wrap it in list: list(enumerate(items)) gives a list of (index, value) tuples.
Want to write cleaner loops? Our free beginner Python course shows you how.