ZetCode

Python enumerate Function

Last modified April 11, 2025

This comprehensive guide explores Python's enumerate function, which adds a counter to iterables. We'll cover basic usage, practical examples, and advanced techniques for efficient iteration with indices.

Basic Definitions

The enumerate function returns an enumerate object that produces a sequence of tuples. Each tuple contains an index and the corresponding value from the iterable.

Key characteristics: works with any iterable, accepts optional start parameter, memory efficient (lazy evaluation), and returns an iterator in Python 3.

Basic Iteration with Index

Here's simple usage showing how enumerate provides both index and value during iteration. This eliminates manual counter management.

basic_enumerate.py
fruits = ['apple', 'banana', 'cherry']

# Without enumerate
for i in range(len(fruits)):
    print(f"Index {i}: {fruits[i]}")

# With enumerate (cleaner)
for i, fruit in enumerate(fruits):
    print(f"Index {i}: {fruit}")

The first loop uses traditional index-based iteration. The second demonstrates enumerate's cleaner approach by directly unpacking index-value pairs.

enumerate makes code more readable and less error-prone by eliminating manual index handling and off-by-one errors.

Custom Start Index

enumerate accepts an optional start parameter to begin counting from a specific number. This is useful for non-zero-based indices.

start_parameter.py
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

# Start counting from 1
for day_num, day_name in enumerate(days, start=1):
    print(f"Day {day_num}: {day_name}")

# Output:
# Day 1: Mon
# Day 2: Tue
# Day 3: Wed
# ...

This example shows how to create human-friendly numbering starting from 1 instead of 0. The start parameter works with any integer value.

This feature is particularly useful when displaying indices to users who might expect numbering to start at 1 rather than 0.

Enumerate with Dictionaries

While dictionaries are iterable, enumerate works differently with them compared to sequences. This example demonstrates the behavior.

dict_enumerate.py
person = {'name': 'John', 'age': 30, 'city': 'New York'}

# Enumerate over keys by default
for i, key in enumerate(person):
    print(f"Index {i}: Key={key}, Value={person[key]}")

# Enumerate over items
for i, (key, value) in enumerate(person.items()):
    print(f"Index {i}: {key}={value}")

The first loop enumerates dictionary keys. The second demonstrates enumerating key-value pairs using items() for more complete information.

Remember that dictionary iteration order was arbitrary before Python 3.7, but now maintains insertion order.

File Processing with Line Numbers

enumerate is particularly useful when processing files to track line numbers. This example shows error reporting with line numbers.

file_processing.py
try:
    with open('data.txt') as f:
        for line_num, line in enumerate(f, start=1):
            line = line.strip()
            if not line:
                continue
            print(f"Processing line {line_num}: {line}")
except FileNotFoundError:
    print("Error: File not found")

This code reads a file line by line while tracking line numbers. Empty lines are skipped, and each processed line is displayed with its number.

The line numbers are valuable for error messages, making it easier to locate issues in the input file.

Creating Dictionaries from Enumerate

enumerate can help create dictionaries where values are mapped to their positions. This example builds a word-to-index mapping.

dict_creation.py
words = ['apple', 'banana', 'cherry', 'date']

# Create dictionary mapping words to their indices
word_index = {word: i for i, word in enumerate(words)}
print(word_index)
# Output: {'apple': 0, 'banana': 1, 'cherry': 2, 'date': 3}

# With custom start
word_index_1 = {word: i for i, word in enumerate(words, start=1)}
print(word_index_1)
# Output: {'apple': 1, 'banana': 2, 'cherry': 3, 'date': 4}

This dictionary comprehension uses enumerate to efficiently create a lookup table. The second version shows how to adjust the numbering scheme.

Such mappings are common in natural language processing and data preprocessing pipelines.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.