Number a list without counting by hand.
Use enumerate to pair each item with its position.
A list can become a small checklist.
Suppose you have three things to pack. Printing them as a numbered list makes the output easier to discuss: “check item two” is clearer than pointing at a line. You could write the numbers yourself, but then adding an item means updating the numbering.
Python’s enumerate() supplies a number alongside each list item. The loop below uses start=1 because this is a checklist for a person. Python list indexes normally start at zero, but the display numbers here can start where you choose.
List → checklist
cable · notebook · pen
1. cable 2. notebook 3. pen
Names make the loop readable.
On each pass, number receives the next count and item receives the next value. The f-string combines them into a readable line. Adding another value to the list makes the loop print another line automatically.
The displayed number does not change how Python indexes the original list. With start=1, the first printed item is numbered 1, but items[0] still refers to that first value. Keep display labels and list positions separate in your thinking.
items = ["cable", "notebook", "pen"]
for number, item in enumerate(items, start=1):
print(f"{number}. {item}")Make it yours.
- Run the example and read its three lines.
- Add "charger" to the list and run it again.
- Change start=1 to start=5 and observe the labels.
What changed when you tried it?
A little nudge
Keep the print line indented underneath the for line.
What to look for
The original output is 1. cable, 2. notebook, and 3. pen on separate lines. Adding charger adds line 4.
Take it one small step further
Reorder the list and notice that the numbers follow the new order.
Let the list supply the items and the loop supply the numbers.
Read more: Python · enumerate ↗
No submission needed. This one is just for you.