Use enumerate(items) in a Python loop to get each item with a counter. The counter starts at zero by default. For a numbered list that starts at one, write for number, value in enumerate(items, start=1):. This works on files and generators as well as lists.
steps = ["Collect", "Check", "Summarize"]
for number, title in enumerate(steps, start=1):
print(f"{number}. {title}")
The output labels run from 1. Collect to 3. Summarize. Python supplies each pair as a tuple. The two names in the loop header unpack that pair.
Choose a zero-based index or a display number
The default counter lines up with list indexes when enumeration starts at the beginning of the full list. The correspondence is easy to lose when you use a different start, enumerate a slice, or deal with a generator. A display number can be called line_number or label. Use index when it really can be used to look up a slot.
items = ["draft", "review", "publish"]
for index, value in enumerate(items):
assert items[index] == value
for label, value in enumerate(items[1:], start=2):
print(label, value)
The second loop prints “2 review” and “3 publish.” The labels were chosen to match the original numbered list, even though the slice's own positions are zero and one. That distinction is worth making in the code if the output is used to point someone back to a larger source.
Keep source positions when you filter
Put the filter inside the loop if the original position matters. Filter first if you want the accepted results numbered consecutively.
lines = ["summary", "", "findings"]
source_lines = [(number, line) for number, line in enumerate(lines, 1) if line]
accepted = [(number, line) for number, line in enumerate(filter(None, lines), 1)]
assert source_lines == [(1, "summary"), (3, "findings")]
assert accepted == [(1, "summary"), (2, "findings")]
An error message about a source file usually needs the original line number. A table of accepted jobs usually needs a clean display sequence. The order of filtering and numbering encodes that decision.
Work with one-pass inputs
enumerate returns an iterator. It asks for a new input item each time you ask for a pair. Calling list(enumerate(lines)) collects all the pairs at once. For a large file, loop directly over the enumerated lines so you can process each pair as it arrives.
Wrapping an iterator halfway through starts a fresh counter for the remaining values. For example, call next(reader) to skip a header and then use enumerate(reader, start=2) if you want the first remaining record numbered two. The start value labels records. With a CSV reader, quoted fields can span physical lines, so a record number may differ from a file line number.
Decide whether you need enumerate at all
For a plain traversal, loop over the values directly. For a search where you need the first matching position, enumerate and break when you find it. For unrelated streams that need to be read together, zip expresses pairing between the streams, while enumerate contributes an independent counter. Use a small empty input and a two-item input to verify numbering before you rely on it in a report or error message.