The enumerate() function in Python wraps any iterable and returns an iterator that yields pairs of (index, item), so you can loop over values and their positions at the same time. It replaces the manual range(len(items)) pattern and keeps loops short and readable.
It is a built-in, available in every Python 3 version, and works with lists, tuples, strings, files, generators and any other iterable.
start sets the first index, which defaults to 0. Each iteration produces a tuple that you normally unpack in the for statement.
The return value is an enumerate object, which is lazy: it produces pairs on demand and does not copy the data. Wrap it in list() to see everything at once:
Use start when numbering should begin at 1, for example for human-readable output. Pass it as a keyword for clarity.
start only changes the counter. It does not skip items or change which element you read.
The older approach works but is noisier and easier to get wrong:
| Approach | Needs indexing | Works on generators | Readability |
|---|---|---|---|
range(len(seq)) | Yes | No | Lower |
| Manual counter variable | No | Yes | Lower, extra state |
enumerate(seq) | No | Yes | High |
Because enumerate works on any iterable, it also works on things that cannot be indexed, such as files and generators:
Combine it with zip. Note the parentheses when unpacking the nested pair:
Strings and tuples are iterables too, so the same pattern applies. This is handy for tasks such as locating characters or validating rows of data.
When reporting errors in input data, enumerate(rows, start=1) lets you print the row number a user would see in a spreadsheet or file, which is more helpful than a zero-based index.
enumerate object can be consumed only once. Create a new one to loop again.for pair in enumerate(x) gives tuples, which is rarely what you want.for item in items is simpler.If you need to change items in place, assign through the index:
Assigning to this index is safe because the list length does not change.
enumerate return a list?No. It returns a lazy iterator. Use list(enumerate(x)) if you need a list of pairs.
enumerate on a dictionary?Yes. It iterates over the keys by default. Use enumerate(d.items()) and unpack as for i, (key, value) in ... to get both.
enumerate faster than range(len())?The difference is usually negligible. Choose enumerate for clarity and for working with any iterable, not for speed.
Yes, start accepts any integer, such as enumerate(items, start=-1).
enumerate(iterable, start=0) gives you index and value together in a clean, lazy loop. Prefer it over range(len()) and manual counters, set start=1 for numbered output, and combine it with zip or comprehensions when you need positions. Avoid changing the sequence's length while looping.