PythonArraysBeginner

enumerate()

Loops with both index and value without manual counters.

Review the syntaxStudy the examplesOpen the coding app
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

iterableIterable[Any] · Items to iterate over.
start (optional)int · Starting index (default 0).

See it in practice

Examples

1

Standard List Enumeration

tasks = ['clean', 'code', 'test']
for index, task in enumerate(tasks):
    print(f'Task {index}: {task}')
Output:
Task 0: clean Task 1: code Task 2: test

The most common use case, providing a zero-based index alongside each list item.

2

Custom Start Index for Human-Readable Labels

winners = ['Alice', 'Bob', 'Charlie']
for rank, name in enumerate(winners, start=1):
    print(f'Place #{rank}: {name}')
Output:
Place #1: Alice Place #2: Bob Place #3: Charlie

Using the start parameter to shift the count for display purposes without modifying logic.

3

Enumerating Over String Characters

word = 'Python'
for i, char in enumerate(word):
    if char.isupper():
        print(f'Uppercase found at index {i}')
Output:
Uppercase found at index 0

Demonstrates that enumerate works on any sequence, including strings.

Debug faster

Common Errors

1

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 tuple
2

TypeError

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 iterable

Runtime support

Compatibility

Python 3.8+

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.