Python 3 reference · Built-in Functions

Python enumerate() Function

Loop over an iterable and get each item together with its index, as (index, item) pairs. The start number is configurable.

Python 3 Since 3.0

Syntax

enumerate(iterable, start=0)

Parameters

ParameterDescription
iterable Any iterable: a list, string, file, generator, …
start optional The number to count from. Default: 0

Return value

An iterator of (index, item) tuples.

Examples

Index and item in a for loopTry it yourself
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
    print(i, fruit)

Output

0 apple
1 banana
2 cherry
Count from 1Try it yourself
for rank, name in enumerate(["Ada", "Linus", "Grace"], start=1):
    print(f"{rank}. {name}")

Output

1. Ada
2. Linus
3. Grace
Find every position of a valueTry it yourself
words = ["red", "green", "blue", "green"]
print([i for i, w in enumerate(words) if w == "green"])
print(list(enumerate("ab")))

Output

[1, 3]
[(0, 'a'), (1, 'b')]

Common mistakes

Avoid for i in range(len(items)): followed by items[i]. enumerate() is shorter, clearer and works with any iterable, not just lists.

Version history

VersionChange
Python 3.0Added