A restaurant menu displays numbered choices.
What You Need to Understand
enumerate(iterable, start=0) yields pairs of an index and a value as you loop. Set start=1 to display human-friendly numbering without changing the underlying list indexes.
When to Use It
A restaurant menu displays numbered choices.
Step-by-Step Python Example
dishes = ["Soup", "Salad", "Pasta"]
for number, dish in enumerate(dishes, start=1):
print(f"{number}. {dish}")
How the Example Works
The index begins at 1 for display, and each pair contains the current number and dish.
Example Output
1. Soup
2. Salad
3. Pasta
10 a
11 b
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
for number, item in enumerate(["a", "b"], start=10):
print(number, item)
Important Behavior and Edge Cases
enumerate does not change the actual index positions of the source list. If you need random access to a list element, actual positions still begin at zero.
Try It Yourself
Number four tasks starting at 10.
Common Beginner Mistakes
enumeratedoes not change the actual index positions of the source list. If you need random access to a list element, actual positions still begin at zero.- Overlooking how an iterable is consumed, how element positions are chosen, or whether a function creates a new list.
Key Takeaways
enumerate(iterable, start=0)yields pairs of an index and a value as you loop.- The index begins at 1 for display, and each pair contains the current number and dish.
enumeratedoes not change the actual index positions of the source list.