enumerate() is a built-in Python function that turns any iterable into an iterator of (index, value) pairs — making it the cleanest, most Pythonic way to loop over sequences while tracking position. In 2026, enumerate() remains indispensable across data science (index-aware processing in pandas/Polars/Dask), software engineering (list comprehension indexing, zip-like operations), and functional pipelines — offering zero-overhead, readable, and memory-efficient enumeration with optional start index and full support in list comprehensions, Dask Bags, NumPy iteration, and async iterators.
Here’s a complete, practical guide to using enumerate() in Python: basic looping, start parameter, real-world patterns (earthquake data indexing, ranked results, parallel processing), and modern best practices with type hints, performance, unpacking, and integration with Dask/Polars/pandas/NumPy.
Basic enumerate() — loop with automatic 0-based index.
fruits = ['apple', 'banana', 'cherry', 'date']
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
# 0: apple
# 1: banana
# 2: cherry
# 3: date
Custom start index & unpacking — powerful for 1-based lists or custom offsets.
# Start from 1 (common for ranked results)
for rank, fruit in enumerate(fruits, start=1):
print(f"Rank {rank}: {fruit}")
# Rank 1: apple
# Rank 2: banana
# ...
# Unpack in list comprehension
ranked = [f"{i+1}. {f}" for i, f in enumerate(fruits)]
print(ranked)
# ['1. apple', '2. banana', '3. cherry', '4. date']
# Multiple iterables with enumerate
names = ['Alice', 'Bob', 'Charlie']
scores = [95, 87, 92]
for i, (name, score) in enumerate(zip(names, scores), 1):
print(f"{i}. {name}: {score}")
Real-world pattern: earthquake catalog processing — index-aware ranking & filtering.
import pandas as pd
df = pd.read_csv('earthquakes.csv')
# Rank strongest events with enumerate
strong = df[df['mag'] >= 7.0].sort_values('mag', ascending=False)
ranked_strong = [
f"#{i+1}: Mag {row['mag']:.1f} - {row['place']}"
for i, row in enumerate(strong.itertuples(), start=1)
]
print("Top 5 strongest earthquakes:")
print("\n".join(ranked_strong[:5]))
# Dask version (large data)
import dask.dataframe as dd
ddf = dd.read_csv('large_earthquakes.csv', assume_missing=True)
strong_dask = ddf[ddf['mag'] >= 7.0].nlargest(10, 'mag').compute()
for i, row in enumerate(strong_dask.itertuples(), 1):
print(f"#{i}: {row.place} (Mag {row.mag:.1f})")
Best practices for enumerate() in Python & data workflows. Prefer enumerate() over range(len(seq)) — cleaner, safer, more Pythonic. Modern tip: use Polars pl.col('idx').rank() — for ranking; Dask for distributed enumeration. Use start=1 — for 1-based indexing (reports, UI). Unpack tuples — for i, value in enumerate(seq). Use in list comprehensions — [(i, v) for i, v in enumerate(seq)]. Add type hints — from typing import Iterable, Tuple; def with_index(seq: Iterable[T]) -> Iterable[Tuple[int, T]]. Use with zip — enumerate(zip(a, b)) for multi-sequence indexing. Avoid modifying list during enumeration — use list comprehension instead. Use enumerate() with Dask Bags — bag.enumerate() for parallel indexing. Profile performance — enumerate() has near-zero overhead. Use itertools.islice(enumerate(seq), 10) — for first N with index. Use enumerate(reversed(seq)) — reverse with indices. Use enumerate(df.itertuples()) — pandas row indexing. Use enumerate(ddf.to_delayed()) — Dask delayed objects. Use enumerate() in async for — with async generators.
enumerate() pairs each element with its index — 0-based or custom start, perfect for ranked output, parallel processing, and index-aware transformations. In 2026, use start=1 for reports, unpack tuples, combine with zip/filter/map, and integrate with Dask/Polars/pandas for data pipelines. Master enumerate(), and you’ll write cleaner, safer, more readable iteration code for any collection.
Next time you loop over a sequence and need the index — use enumerate(). It’s Python’s cleanest way to say: “Give me each item — and tell me where it came from.”