enumerate()
Loops with both index and value without manual counters.
for index, item in enumerate(iterable, start=0):
...This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Loops with both index and value without manual counters.
The enumerate() function is a built-in Python utility that returns an iterator yielding pairs of counts and values from an iterable. Unlike manual index management using an external counter, enumerate() is implemented in C for high performance, minimizing the overhead typically associated with loop-based incrementing. It accepts an iterable and an optional 'start' parameter, which defaults to 0. Because it returns an iterator, it operates with O(1) auxiliary space complexity, making it ideal for memory-efficient processing of massive datasets or infinite streams. It is technically superior to the 'range(len(sequence))' idiom because it avoids the redundant cost of secondary item lookup via the __getitem__ method. Note that enumerate() only works on iterables; passing a non-iterable object will raise a TypeError immediately.
Quick reference
Syntax
for index, item in enumerate(iterable, start=0):
...
Inputs
Parameters
See it in practice
Examples
Standard List Enumeration
tasks = ['clean', 'code', 'test']
for index, task in enumerate(tasks):
print(f'Task {index}: {task}')Task 0: clean Task 1: code Task 2: test
The most common use case, providing a zero-based index alongside each list item.
Custom Start Index for Human-Readable Labels
winners = ['Alice', 'Bob', 'Charlie']
for rank, name in enumerate(winners, start=1):
print(f'Place #{rank}: {name}')Place #1: Alice Place #2: Bob Place #3: Charlie
Using the start parameter to shift the count for display purposes without modifying logic.
Enumerating Over String Characters
word = 'Python'
for i, char in enumerate(word):
if char.isupper():
print(f'Uppercase found at index {i}')Uppercase found at index 0
Demonstrates that enumerate works on any sequence, including strings.
Debug faster
Common Errors
ValueError
Cause: Attempting to unpack only one variable when enumerate() yields a tuple of (index, value).
Fix: Provide two variables in the for-loop header or access the tuple by index.
items = ['a', 'b']
for i in enumerate(items):
print(i + 1) # Raises TypeError: can only concatenate tuple (not "int") to tupleTypeError
Cause: Passing a non-iterable object, such as an integer or a custom object without an __iter__ method.
Fix: Ensure the first argument is a collection (list, tuple, str) or a generator.
for i, val in enumerate(100):
print(val) # Raises TypeError: 'int' object is not iterableRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Loops with both index and value without manual counters.
iterable: Items to iterate over. start: Starting index (default 0).
ValueError: Provide two variables in the for-loop header or access the tuple by index. TypeError: Ensure the first argument is a collection (list, tuple, str) or a generator.