enumerate
Number the items of an iterable while iterating over it.
Python 3.13
enumerate(iterable, start=0)- =default value
- iterable
- The items to number.Iterable
- start
- The number to start counting from, 0 by default.intOptional
Return value
An iterator of (number, item) pairs.
Examples
Numbering list items
Python 3.13
print(list(enumerate(['a', 'b', 'c'])))[(0, 'a'), (1, 'b'), (2, 'c')]
Numbering from one in a loop
Python 3.13
for number, fruit in enumerate(['apple', 'pear'], start=1):
print(number, fruit)1 apple 2 pear
Details
It is more convenient than looping over range(len(a)): you get both the number and the item at once. start changes only the numbers; iteration still begins with the first item.
