Home / Python / Reference / Built-in functions

Python enumerate() function

Loop over items together with their position.

Syntax

enumerate(iterable, start=0)

Gives (index, item) pairs, so you never need range(len(items)). Set start=1 to number from 1.

Parameters

iterablewhat to loop over
startthe first number (default 0)

Return value

an enumerate object (an iterator of pairs)

Example

Code

fruits = ["apple", "kiwi", "mango"]
for i, fruit in enumerate(fruits):
    print(i, fruit)
for n, fruit in enumerate(fruits, start=1):
    print(f"{n}. {fruit}")
print(list(enumerate("ab")))

Output

0 apple
1 kiwi
2 mango
1. apple
2. kiwi
3. mango
[(0, 'a'), (1, 'b')]

Related

Learn it step by step in the free lesson Loops, with exercises that check your answers.