all Python 3Loopsfunction
enumerate()
iteration
Built-in function that adds a counter to an iterable. It returns (index, value) tuples. The start parameter sets the initial index.
Syntax
enumerate(iterable, start=0)
enumerate frees you from the clumsy range(len(...)) walk with manual indexing — it hands you the (index, value) pair directly. It returns a lazy iterator, not a list, so wrap it in list() only when you actually need the whole result at once.
Examples
fruits = ["яблоко", "банан", "вишня"]
for i, fruit in enumerate(fruits):
print(i, fruit) # → 0 яблоко / 1 банан / 2 вишня
for i, fruit in enumerate(fruits, start=1):
print(f"{i}. {fruit}") # → 1. яблоко / 2. банан / 3. вишня
lst = [10, 20, 30, 40]
for i, v in enumerate(lst):
if v == 20:
print("индекс 20:", i) # → индекс 20: 1
nums = [5, 3, 8, 1]
max_i, max_v = max(enumerate(nums), key=lambda x: x[1])
print(max_i, max_v) # → 2 8
print(dict(enumerate("abc"))) # → {0: 'a', 1: 'b', 2: 'c'}
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy