enumerate()
enumerate(iterable, start=0) enumerate object · Updated March 13, 2026 · Built-in Functions The enumerate() function takes an iterable and returns an enumerate object that yields pairs of (index, value) during iteration. It replaces the common pattern of manually initializing and incrementing a counter variable inside a loop, making code more readable and less error-prone.
Syntax
enumerate(iterable, start=0)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable | iterable | — | Any object that supports iteration (list, tuple, string, dict, file object, generator, etc.) |
start | int | 0 | The integer value from which to start counting. The first element will have index start, the second will have start + 1, and so on. |
Returns: An enumerate object, which is a lazy iterator yielding tuples of (index, value). Convert to a list or tuple to materialize all pairs, or iterate directly.
Examples
Basic usage
The most common use case is iterating over a sequence while tracking the index:
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# 0: apple
# 1: banana
# 2: cherry
Custom start value
The start parameter lets you begin counting from any integer:
for rank, name in enumerate(["Alice", "Bob", "Charlie"], start=1):
print(f"#{rank} {name}")
# #1 Alice
# #2 Bob
# #3 Charlie
This is particularly useful for 1-based indexing, human-readable output, and matching external data sources that use different indexing conventions.
Converting to a list of tuples
For scenarios requiring all index-value pairs at once:
letters = "abcde"
indexed = list(enumerate(letters))
print(indexed)
# [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
Using with a tuple
Works with any iterable, not just lists:
point = (10, 20, 30)
for i, coord in enumerate(point):
print(f"Dimension {i}: {coord}")
# Dimension 0: 10
# Dimension 1: 20
# Dimension 2: 30
Common Patterns
Processing file contents with line numbers
When reading files, enumerate() with start=1 provides natural line numbers:
with open("config.txt") as f:
for line_num, line in enumerate(f, start=1):
print(f"Line {line_num}: {line.strip()}")
Building a dictionary from a list
Create a mapping from values to their positions:
names = ["Alice", "Bob", "Charlie"]
name_to_index = {name: i for i, name in enumerate(names)}
print(name_to_index)
# {'Alice': 0, 'Bob': 1, 'Charlie': 2}
Enumerating in reverse
Combine with reversed() to iterate backwards while maintaining indices:
colors = ["red", "green", "blue"]
for i, color in reversed(list(enumerate(colors))):
print(f"{i}: {color}")
# 2: blue
# 1: green
# 0: red
With list comprehension for transformation
Transform a list while preserving index information:
values = [10, 20, 30, 40]
doubled_with_index = [(i, v * 2) for i, v in enumerate(values)]
print(doubled_with_index)
# [(0, 20), (1, 40), (2, 60), (3, 80)]
See Also
-
built-in::zip — iterate over multiple iterables in parallel, pairing elements by position
-
built-in::range — generate a sequence of integers, often used as an alternative to enumerate
-
built-in::reversed — iterate over elements in reverse order while maintaining index context