python / beginner
Snippet
Using Enumerate
The enumerate() function adds a counter to an iterable and returns it, allowing you to track the index while looping.
snippet.py
1
2
3
tasks = ["Clean", "Cook", "Read"]for i, task in enumerate(tasks, start=1):print(f"{i}. {task}")
Breakdown
1
for i, task in enumerate(tasks, start=1):
Loops through tasks, assigning the count to 'i' and the item to 'task', starting at 1.