Need each loop iteration’s position alongside its item? Use enumerate instead of building a range from len(iterable). It produces a cleaner, more Pythonic loop. section 1.4.1 section 5.0
enumerate(iterable) creates an iterator that yields tuples of (counter, item). The counter starts at 0 by default; pass start=1 or another value to choose the first counter. Tuple unpacking assigns both values directly. section 5.3.1
Number tasks while iterating
tasks = ["Homework", "Laundry", "Museum"] for task_i, task in enumerate(tasks, start=1): print(f"Task {task_i}: {task}") # Task 1: Homework # Task 2: Laundry # Task 3: Museum section 5.3.1
Format structured task records
for task_i, task in enumerate(tasks, start=1): print(f"Task {task_i}: {task.title:<10}") print(f"{task.description:<18} {task.urgency}") This pattern combines enumeration, tuple unpacking, and formatted output while keeping the loop readable. section 5.3.1
Prefer enumerate when a loop needs both an item and its position:
- Use enumerate(items) when zero-based counters are appropriate. section 5.3.1
- Use enumerate(items, start=1) for human-facing numbering. section 5.3.1
- Avoid manually combining range(len(items)), indexing, and counter arithmetic when enumerate expresses the intent directly. section 1.4.1 section 5.3.1


