enumerate() is the built-in that pairs each item from an iterable with a counter while you loop. The official docs define enumerate(iterable, start=0) as returning an enumerate object whose __next__ hands you a tuple (count, value) on each step.
Do not confuse enumerate() with enum. enumerate() is for loops: you get an index and the current item. The enum module defines named constants—see Python Enum.
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)You should see 0 apple, then 1 banana, then 2 cherry on three lines. That is the usual reason to use it: readable access to both position and value without manual index math.
This article stays at that level—patterns for lists, strings, tuples, dicts, small edits in place, zip, comprehensions, and how enumerate compares to range(len(...)).
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
What does enumerate() do in Python?
It wraps any iterable so each step of a for loop gives you (count, item). The counter starts at 0 unless you pass start. The original iterable is not copied or changed by enumerate itself. For loop basics, see Python for loop.
The call shape is enumerate(iterable, start=0): iterable is anything you can loop over (list, tuple, string, dict keys, file lines, and so on), and start is an optional integer first index (often 1 for human-facing labels). The return value is an iterator—consume it in a loop or wrap it in list() if you need all (index, value) pairs at once.
A fence that only contained enumerate(iterable, start=0) would “run” without printing anything, because building the iterator does not display pairs by itself. The block below keeps the signature in a comment and prints each step:
# enumerate(iterable, start=0)
letters = ["x", "y"]
for index, ch in enumerate(letters):
print(index, ch)You should see 0 x and 1 y on two lines.
Use enumerate() in a for loop
Unpack the two tuple parts into index and value (names are up to you):
names = ["Ada", "Bob"]
for i, name in enumerate(names):
print(i, name.upper())You should see 0 ADA and 1 BOB.
Nested loops (optional)
You can nest enumerate when you have nested sequences—just use clear variable names so i, j do not blur together.
matrix = [[10, 20], [30, 40]]
for r, row in enumerate(matrix):
for c, value in enumerate(row):
print(r, c, value)You should see four lines (0,0,10) through (1,1,40).
Start enumerate() from 1
Pass start=1 when the counter is for people (lines, ranks) instead of zero-based storage indexes.
items = ["first", "second"]
for line_no, text in enumerate(items, start=1):
print(line_no, text)You should see 1 first then 2 second.
Use enumerate() with a list
scores = [10, 20, 30]
for idx, score in enumerate(scores):
print(idx, score)You should see three index-score pairs in order.
Use enumerate() with a string
Iteration yields each character; the index is the position in the string.
for i, ch in enumerate("hi"):
print(i, repr(ch))You should see 0 'h' and 1 'i'.
Use enumerate() with a tuple
Tuples iterate like lists here:
point = (3, 4, 5)
for axis, value in enumerate(point):
print(axis, value)You should see three lines 0 3, 1 4, 2 5.
Use enumerate() with a dictionary
By default for x in d walks keys, so enumerate(d) numbers those keys:
user = {"name": "Alice", "age": 30, "city": "Delhi"}
for index, key in enumerate(user):
print(index, key, user[key])You should see three lines with indices 0, 1, 2 and each key with its value. For key and value together without extra indexing, prefer items():
user = {"name": "Alice", "age": 30, "city": "Delhi"}
for index, (key, value) in enumerate(user.items()):
print(index, key, value)Same data, clearer unpacking when you need both fields every time. For more on dicts, see Python dictionary example.
Modify list items using enumerate()
Use the index to assign back when you must update the list in place.
nums = [1, 2, 3]
for i, n in enumerate(nums):
nums[i] = n * 10
print(nums)You should see [10, 20, 30]. If you only need transformed data, a list comprehension is often simpler; enumerate shines when the write targets the original container by index.
Use enumerate() with zip()
Pair two parallel sequences with aligned indices:
names = ["Ada", "Bob"]
scores = [100, 85]
for i, (name, score) in enumerate(zip(names, scores)):
print(i, name, score)You should see 0 Ada 100 and 1 Bob 85.
Use enumerate() in a list comprehension
Sometimes you build a new list from index and value in one expression. See list comprehension examples for more map-and-filter patterns in one line:
words = ["a", "bb", "ccc"]
labeled = [f"{i}:{w}" for i, w in enumerate(words)]
print(labeled)You should see ['0:a', '1:bb', '2:ccc'].
enumerate() vs range(len())
Prefer enumerate when you need both index and element—it keeps the value in the loop header and avoids repeated subscripting.
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(index, fruit)
for index in range(len(fruits)):
print(index, fruits[index])Both fragments print the same lines here; the first is the usual style. Use range(len(x)) when you truly need only indices, or when you are stepping by strides range(0, len(x), 2) in ways enumerate does not cover without extra logic.
Use enumerate() with a file
Open a text file and number lines without managing a manual counter—handy for error messages that cite “line N”.
from io import StringIO
fake_file = StringIO("one\ntwo\n")
for line_no, line in enumerate(fake_file, start=1):
print(line_no, line.strip())You should see 1 one and 2 two. With a real path, use with open("path.txt") as f: the same way.
Common mistakes with enumerate()
- Forgetting to unpack:
for pair in enumerate(items)then misusingpairinstead offor i, x in enumerate(items). - Thinking
enumeratemutates the iterable—it does not; only your loop body might. - Using
enumeratewhen you only need values—plainfor x in itemsis simpler. - Defaulting to
range(len(items))when you also need eachitems[i]—enumerateis clearer. - Omitting
start=1for user-visible numbering when zero would confuse readers. - Mixing up
enumerate()with theenummodule’s named constants—they solve different problems (the loop helper is covered in the introduction above). - Expecting
enumerate(d)to yield dict values; it pairs indices with keys unless you iterated.items().
Python enumerate() quick reference table
| Need | Example |
|---|---|
| Basic loop | for i, x in enumerate(items): |
| Start at 1 | enumerate(items, start=1) |
| Dict keys + index | for i, k in enumerate(d): |
| Dict key + value + index | for i, (k, v) in enumerate(d.items()): |
| Build pairs list | list(enumerate(items)) |
Summary
enumerate(iterable, start=0) gives you (count, item) tuples as you iterate—ideal for indexed loops that still need the current value. Use start for human-friendly line numbers, items() when numbering dict entries with values, and prefer it over range(len(...)) when both index and element matter. It is unrelated to the enum module’s named constants; keep that distinction in mind when you search for “enumeration in Python.”

