Skip to content
techskilldaily.
The archiveNo. 010
A little Python

Number a list without counting by hand.

Use enumerate to pair each item with its position.

2 min read 5 min to try
MAKE ROOM FORoneSMALL SKILL

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

Values

cable · notebook · pen

Displayed lines

1. cable 2. notebook 3. pen

Illustrated expected output.

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.

A small example to try
items = ["cable", "notebook", "pen"]
for number, item in enumerate(items, start=1):
    print(f"{number}. {item}")
Your turn 5 minutes

Make it yours.

  1. Run the example and read its three lines.
  2. Add "charger" to the list and run it again.
  3. 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.

Keep this bit

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.

Another spark of curiosityKeep the column names in sight.