enumerate() in Python: the item and its position, together
"Day 6 was the best day." To say that, a loop needs both the value and which day it was. enumerate() hands you both at once.
The big idea
enumerate(sequence) yields pairs of an index and the item at that index, so a loop can use both without managing a counter.
See it in code
Each pass receives two things, unpacked into two loop variables: the position, then the item:
daily_tees = [42, 57, 38]
for index, tees in enumerate(daily_tees):
print(index, tees)0 42 1 57 2 38
The index starts at 0, matching list positions. No range, no len, and no bracket lookups.
People count days from 1. Pass start=1 and the numbering shifts, while the items stay the same:
daily_tees = [42, 57, 38, 64, 71, 95, 88]
for day, tees in enumerate(daily_tees, start=1):
print(f"Day {day}: {tees} tees")Day 1: 42 tees Day 2: 57 tees Day 3: 38 tees Day 4: 64 tees Day 5: 71 tees Day 6: 95 tees Day 7: 88 tees
start=1 changes only the number you're handed. It doesn't skip the first item or alter the list.
With the position available, a best-so-far search can report which day won, not just how much it sold:
daily_tees = [42, 57, 38, 64, 71, 95, 88]
price = 38
best_day = 0
best_tees = 0
for day, tees in enumerate(daily_tees, start=1):
if tees > best_tees:
best_tees = tees
best_day = day
print(f"Best day: day {best_day}, with {best_tees} tees (${best_tees * price:,})")Best day: day 6, with 95 tees ($3,610)
max(daily_tees) would have found 95, but not that it happened on day 6. Whenever position carries meaning, loop with enumerate.
Position matters in plenty of data: the line number of an error in a file, the index of a base where two sequences differ, the rank of a search result.
Try it yourself
Print only the days that beat a target of 60, with their day numbers. Then loop over two lists at once by using the index from enumerate to read a second, parallel list.
The common mistake
Forgetting to unpack. for item in enumerate(daily_tees) gives you tuples like (0, 42), and arithmetic on them fails. Name both parts: for day, tees in enumerate(...).
What it unlocks
enumerate is a cleaner for loop for lists. The pairs it produces are tuples, unpacked on the spot.