enumerate() Builtin Function


Python - enumerate()

Python enumerate() builtin function takes an iterable object, and returns an enumerate object.

In this tutorial, you will learn the syntax of enumerate() function, and then its usage with the help of example programs.

Syntax

The syntax of enumerate() function is

enumerate(iterable, start=0)

where

ParameterDescription
iterableA sequence.
startstart 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.

Examples

1. Enumerate over a list of strings

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)

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.