enumerate() (Python)

enumerate() lets you loop over a sequence while also getting the index of each item. It pairs each element with a counter, so you don't have to track the position yourself.

Python

Syntax

for index, item in enumerate(iterable):

Examples

Python

for i, color in enumerate(["red", "green"]):
    print(i, color)

Output:

0 red
1 green

Learn it in a lesson

See also

Back to the Dictionary