enumerate()

Getting the position alongside the item, without maintaining a counter or indexing back into the list.

Overview

The two things it replaces

The counter:

i = 0
for name in names:
    print(i, name)
    i += 1

and the index:

for i in range(len(names)):
    print(i, names[i])

Both work. The first has a variable to initialise and remember to increment; forget the increment and the loop runs forever printing zero. The second reads the list twice per pass — once for the length, once per lookup — and puts names[i] where you wanted name.

for i, name in enumerate(names):

says the same thing with neither problem.

enumerate.py

enumerate.py Python 3
Output

                    

enumerate_uses.py

enumerate_uses.py Python 3
Output

                    

Worth knowing

enumerate yields (index, item) tuples; the for unpacks them.
start=1 changes the number it reports, not where it reads from.
It works on any iterable - strings, files, generators - not only lists.
for i in range(len(x)) is the pattern this replaces.

enumerate(): A Practical Guide

enumerate walks a sequence and hands you the position along with the item. It replaces both of the usual workarounds — a counter you increment by hand, and indexing back into the list.

What it actually produces

enumerate yields tuples: (0, "ana"), (1, "bo"). The for i, name on the left is ordinary tuple unpacking, which is why the tuples are invisible in normal use. Loop without unpacking and you see them:

for pair in enumerate(names):  # (0, 'ana')

list(enumerate(x)) is the quickest way to see the whole thing at once.

start= renumbers the label

for n, name in enumerate(names, start=1):

This is for output humans read — line numbers, ranked lists, steps. It changes the number reported, not the position read: with start=1, the item labelled 1 is still names[0]. If you use n to index back into the list you will be off by one, which is a bug that only shows up on the first and last element.

It is lazy, and it takes any iterable

enumerate does not build a list of pairs. It produces them one at a time as the loop asks, so it works on a file, a generator, or a sequence too large to hold in memory. It also works on strings, giving character positions.

When you do not need it

If you never use the index, do not ask for it. for name in names is the shorter, clearer loop, and reaching for enumerate out of habit adds a variable nothing reads.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does `enumerate(['a', 'b'])` yield?

  2. With `enumerate(names, start=1)`, which item does n=1 refer to?

  3. Why prefer enumerate over `for i in range(len(items))`?

Cheat sheet

enumerate()

enumerate walks a sequence and hands you the position along with the item. It replaces both of the usual workarounds — a counter you increment by hand, and indexing back into the list.

PYTHON · vizlearn.in/python/enumerate_function.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.