Without enumerate(), getting both an index and a value inside a for loop means manually tracking a counter or indexing with range and len. enumerate() instead returns a lazy iterator of index-item tuples starting at 0, or at whatever number you pass as start, and is almost always unpacked directly in the loop header.
1Understanding enumerate()
Without enumerate(), getting both an index and a value inside a for loop means manually tracking a counter or indexing with range and len. enumerate() instead returns a lazy iterator of index-item tuples starting at 0, or at whatever number you pass as start, and is almost always unpacked directly in the loop header.
Pass start=1 to enumerate() instead of the default of 0 when you want human-friendly, 1-based numbering, for example when displaying a numbered list in a UI or CLI output.
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, fruit)2Practical Example
Here is a real-world application of enumerate() showing how it is used in production Python code.
tasks = ["Design", "Build", "Ship"]
for step_num, task in enumerate(tasks, start=1):
print(f"Step {step_num}: {task}")3Best Practices
Follow these guidelines when working with enumerate():
1. Use enumerate(items) instead of range(len(items)) whenever you need both the index and the value
2. Pass start=1 for 1-based counting instead of adding 1 to the index manually inside the loop
3. Unpack the tuple directly in the for statement rather than indexing into it afterward
Tip: Pass start=1 to enumerate() instead of the default of 0 when you want human-friendly, 1-based numbering, for example when displaying a numbered list in a UI or CLI output.
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, fruit)