Codectionary / Developer documentation / Python

enumerate(): counters and values

Pair each item with a running counter without managing a separate index variable.

How it works

enumerate is a built-in function that returns an iterator of (counter, value) pairs. A for loop unpacks each pair into two names. The default counter begins at zero; start changes the displayed counter, not which items are visited.

It works with any iterable, including generators. Items are requested as the loop advances, so enumerate does not first copy the entire input. The counter describes the iteration order rather than an inherent identifier belonging to the item.

Choices and common mistakes

Use direct iteration when you only need values. Use enumerate when a position or line number has a purpose.

Changing the starting counter does not skip input. Do not use a one-based counter directly as a zero-based list index.

Try it yourself

Number a task list from one. Then try an empty list and a list containing the same task twice. Explain why the duplicate tasks still receive different counters.

Syntax

for position, value in enumerate(items, start=1):
    print(position, value)

Examples

Number a menu

Present two choices using human-friendly numbering.

choices = ["Read", "Practise"]
for number, choice in enumerate(choices, start=1):
    print(f"{number}. {choice}")
  1. The list keeps the desired display order. start=1 makes the first counter one; it does not change the list.
  2. The loop unpacks each pair into number and choice. The f-string combines those values into one line.
  3. Use numbered output for a command-line menu, while validating any later user selection separately.
  4. Expected output: 1. Read 2. Practise

Report blank line positions

Find empty entries without losing their original positions.

lines = ["title", "", "body", ""]
for number, line in enumerate(lines, start=1):
    if not line:
        print(f"Blank line {number}")
  1. Enumeration happens before filtering, so the counters still describe positions in the original input.
  2. An empty string is false in a condition. Only those entries reach print; a whitespace-only string would need strip() to be treated as blank.
  3. This pattern can give useful line numbers in a text-file validation report.
  4. Expected output: Blank line 2 Blank line 4

Consume a one-shot input

Observe that the iterator is exhausted after traversal.

pairs = enumerate(["red", "blue"], start=10)
print(next(pairs))
print(list(pairs))
print(list(pairs))
  1. next consumes the first pair. list then consumes only the remaining pair, rather than restarting the enumeration.
  2. The final empty list means there are no pairs left. Create a fresh enumerate object to traverse a reusable input again.
  3. Remember this when passing an enumerator between functions: each consumer advances the same iterator.
  4. Expected output: (10, 'red') [(11, 'blue')] []

Best practices

  • Use direct iteration when you only need values. Use enumerate when a position or line number has a purpose.
  • Changing the starting counter does not skip input. Do not use a one-based counter directly as a zero-based list index.

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading