Contents
Python – enumerate()
Python enumerate() builtin function takes an iterable object, and returns an enumerate object.
Syntax
The syntax of enumerate()
function is
enumerate(iterable, start=0)
where
iterable
is a sequencestart
is an optional integer value. Default value is 0. If any other value is passed, then the __next__() method of the iterator returned by enumerate() function returns a tuple containing the count and the value from iterable. count starts at the given start value for the first item in the iterable.
Example
In the following program, we take a list of strings fruits
, and enumerate over them using enumerate() builtin function.
Python Program
fruits = ['apple', 'banana', 'cherry']
for item in enumerate(fruits):
print(item)
Run Output
(0, 'apple')
(1, 'banana')
(2, 'cherry')
Summary
In this tutorial of Python Examples, we learned the syntax of enumerate() builtin function, and how to enumerate a given iterable, using enumerate() function, with examples.