Python enumerate() Method – [With Example]

In this tutorial, we will learn about python enumerate() method and its uses.

Python enumerate() Method

WHAT IS PYTHON ENUMERATE() METHOD?

The enumerate() method adds a counter to an iterable and returns its enumerated object.

The syntax of enumerate() is:

enumerate(iterable, start=0)

PYTHON ENUMERATE() PARAMETERS

enumerate() method takes two parameters:

iterable – A sequence, an iterator, or an object that supports iteration.

start – Starts counting from this number. If start is omitted, 0 is taken as a start. This is an Optional.

Let’s check some examples of python enumerate()

Example 1: How Enumerate() Works In Python?

cars = [‘BMW’,’Audi’,’Toyota’]

enumerateCars = enumerate(cars)

print(type(enumerateCars))

print(list(enumerateCars))

# changing the default counter

enumerateCars = enumerate(cars, 10)

print(list(enumerateCars))

The output will be as follows.

<class ‘enumerate’=””>

[(0, ‘BMW’), (1, ‘Audi’), (2, ‘Toyota’)]

[(10, ‘BMW’), (11, ‘Audi’), (12, ‘Toyota’)]</class>

Example 2: Looping with an Enumerate object.

cars = [‘BMW’,’Audi’,’Toyota’]

for item in enumerate(cars):

print(item)

Output:

(0, ‘BMW’)

(1, ‘Audi’)

(2, ‘Toyota’)

RULES OF ENUMERATE()

The enumerate() method takes a collection(e.g. a list) and returns it as an enumerate object.

The enumerate() method adds a counter as the key of the enumerate object.

The official documentation on python enumerate() method from here.

Check Python Memoryview Method here.