01 / 20
0 : a 1 : b 2 : c

Python
enumerate()

Master the Pythonic way to iterate with indices: cleaner loops, fewer bugs, and more readable code.

Python

The Problem with Manual Indexing

When you loop over a list but also need the position of each item, many beginners reach for a manual counter variable:

fruits = ["apple", "banana", "cherry"]
i = 0
for fruit in fruits:
    print(i, fruit)
    i += 1

What Is enumerate()?

enumerate() is a built-in Python function that wraps any iterable and returns an iterator of tuples, each containing an index and the corresponding item.

Input
Any iterable: list, string, tuple, range, file, generator, etc.
Output
An enumerate object yielding (index, item) tuples on each iteration.

How enumerate() Works

INPUT LIST "apple" "banana" "cherry" enumerate() OUTPUT TUPLES (0, "apple") (1, "banana") (2, "cherry") index item unpack for i, fruit in enumerate(fruits): print(i, fruit)

The Syntax

enumerate(iterable, start=0)
iterable
The sequence you want to loop over (required).
start
The number at which the counter begins. Defaults to 0.

Return Type: Lazy Iterator

enumerate() does not build a list in memory. It returns a lazy iterator that produces one (index, item) tuple at a time, on demand.

e = enumerate(["a", "b"])
print(type(e))   # <class 'enumerate'>
print(next(e))   # (0, 'a')
print(next(e))   # (1, 'b')

enumerate() vs range(len())

enumerate() range(len())
Readability ★★★ Pythonic ★ Verbose
Gives item directly? Yes No: must index list[i]
Works on any iterable? Yes No: needs len()
Custom start index? Yes: start=N Manual offset
Risk of off-by-one? Low Higher

How to Apply enumerate()

1
Identify the iterable you want to loop over (list, string, tuple, etc.).
2
Wrap it with enumerate(). Optionally set start= if counting from a value other than 0.
3
Unpack both variables in the for-loop header: for index, item in ...
4
Use index and item inside the loop body: no manual counter, no list[i] lookup.

Example 1: Basic Loop

Python
colors = ["red", "green", "blue"]

for i, color in enumerate(colors):
    print(f"{i}: {color}")
Output
0: red
1: green
2: blue

Example 2: Custom Start

Python
questions = ["What is 2+2?", "Capital of France?"]

for num, q in enumerate(questions, start=1):
    print(f"Q{num}: {q}")
Output
Q1: What is 2+2?
Q2: Capital of France?

Example 3: Finding an Item's Position

Python
scores = [85, 92, 78, 95, 88]

for i, score in enumerate(scores):
    if score > 90:
        print(f"High score at index {i}: {score}")
Output
High score at index 1: 92
High score at index 3: 95

Example 4: Dict Comprehension

Python
letters = ["a", "b", "c"]

index_map = {char: idx
              for idx, char in enumerate(letters)}

print(index_map)
# {'a': 0, 'b': 1, 'c': 2}

Example 5: Enumerating a String

Python
word = "hello"

for pos, ch in enumerate(word):
    print(f"[{pos}] {ch}")
Output
[0] h
[1] e
[2] l
[3] l
[4] o

Common Mistakes

✕ Forgetting to Unpack
Writing for item in enumerate(lst) gives you a tuple, not the item. Always unpack: for i, item in ...
✕ Ignoring start=
Manually adding i + 1 when start=1 would be cleaner and less error-prone.
✕ Using range(len()) Reflexively
Reaching for range(len(lst)) when you need the value too. enumerate() gives both at once.

Summary Cheat Sheet

Pattern Code When to Use
Basic enumerate for i, x in enumerate(lst) Need index + value
Custom start enumerate(lst, start=1) 1-based numbering
Dict comprehension {v: i for i, v in enumerate(lst)} Build index lookup
Conditional filter if condition: use i Find positions
Convert to list list(enumerate(lst)) Need all pairs at once

Question 1 of 5

What does enumerate() return?

Question 2 of 5

What is the output of this code?

for i, ch in enumerate("AB", start=5):
    print(i, ch)

Question 3 of 5

Which of these is the most Pythonic way to print each item with its index?

Question 4 of 5

What happens if you write for x in enumerate(["a", "b"]) without unpacking?

Question 5 of 5

What does this code produce?

result = dict(enumerate(["x", "y", "z"]))
print(result)