Python enumerate() Function

Learn Python enumerate() with simple for loop examples. Use enumerate() to get index and value while looping through lists, strings, tuples, dictionaries, and files.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Python enumerate() Function

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.

python
fruits = ["apple", "banana", "cherry"]

for index, fruit in enumerate(fruits):
    print(index, fruit)
Output

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:

python
# enumerate(iterable, start=0)
letters = ["x", "y"]

for index, ch in enumerate(letters):
    print(index, ch)
Output

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):

python
names = ["Ada", "Bob"]
for i, name in enumerate(names):
    print(i, name.upper())
Output

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.

python
matrix = [[10, 20], [30, 40]]
for r, row in enumerate(matrix):
    for c, value in enumerate(row):
        print(r, c, value)
Output

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.

python
items = ["first", "second"]
for line_no, text in enumerate(items, start=1):
    print(line_no, text)
Output

You should see 1 first then 2 second.


Use enumerate() with a list

python
scores = [10, 20, 30]
for idx, score in enumerate(scores):
    print(idx, score)
Output

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.

python
for i, ch in enumerate("hi"):
    print(i, repr(ch))
Output

You should see 0 'h' and 1 'i'.


Use enumerate() with a tuple

Tuples iterate like lists here:

python
point = (3, 4, 5)
for axis, value in enumerate(point):
    print(axis, value)
Output

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:

python
user = {"name": "Alice", "age": 30, "city": "Delhi"}

for index, key in enumerate(user):
    print(index, key, user[key])
Output

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():

python
user = {"name": "Alice", "age": 30, "city": "Delhi"}

for index, (key, value) in enumerate(user.items()):
    print(index, key, value)
Output

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.

python
nums = [1, 2, 3]
for i, n in enumerate(nums):
    nums[i] = n * 10
print(nums)
Output

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:

python
names = ["Ada", "Bob"]
scores = [100, 85]
for i, (name, score) in enumerate(zip(names, scores)):
    print(i, name, score)
Output

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:

python
words = ["a", "bb", "ccc"]
labeled = [f"{i}:{w}" for i, w in enumerate(words)]
print(labeled)
Output

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.

python
fruits = ["apple", "banana", "cherry"]

for index, fruit in enumerate(fruits):
    print(index, fruit)

for index in range(len(fruits)):
    print(index, fruits[index])
Output

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”.

python
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())
Output

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 misusing pair instead of for i, x in enumerate(items).
  • Thinking enumerate mutates the iterable—it does not; only your loop body might.
  • Using enumerate when you only need values—plain for x in items is simpler.
  • Defaulting to range(len(items)) when you also need each items[i]enumerate is clearer.
  • Omitting start=1 for user-visible numbering when zero would confuse readers.
  • Mixing up enumerate() with the enum module’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 iterate d.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.”


References


Frequently Asked Questions

1. What does Python enumerate() return?

It returns an iterator that yields (index, value) tuples one per item from the iterable; you almost always unpack them in the for loop as for i, x in enumerate(items).

2. Does enumerate() change the original list?

No—it only walks the iterable and counts; it does not mutate lists, strings, or other inputs unless your loop body assigns back into them.

3. When should I use enumerate() instead of range(len())?

Use enumerate() whenever you want both the index and the current element—readers see the value directly; reserve range(len()) for index-only patterns or when you truly need the index without the element in scope.

4. Is enumerate() the same as enum in Python?

No—enumerate() is a built-in for loop counters over iterables; enum is a module for named constant types—see the Python Enum guide linked from this page.

5. Why does enumerate(my_dict) only show keys?

Iterating a dict yields keys by default; use enumerate(d.items()) when you want index plus key and value together.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …