Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Python enumerate() Function



The Python enumerate() function returns enumerated object of the specified iterable. An iterable object can be any data structure like list, tuple, set, string and dictionary, etc. It adds a counter as the key to the enumerated object. This function has one optional parameter which can be used to specify the starting point of the counter.

Syntax

enumerate(iterable, start)

Parameters

iterable Required. specify iterable object like list, tuple, set, string and dictionary, etc.
start Optional. specify starting point of counter. default is zero.

Example:

In the example below, the enumerate() is used to create enumerated object using iterables called MyList, MyString and MyDict.

MyList = [10, 20, 30, 40, 50]
x = enumerate(MyList)
print(list(x))


MyString = "Python"
x = enumerate(MyString, 1)
print(tuple(x))

MyDict = {
   "name": "John",
   "age": 25,
   "city": "London"
}
x = enumerate(MyDict, 1)
print(list(x))

x = enumerate(MyDict.values(), 1)
print(list(x))

The output of the above code will be:

[(0, 10), (1, 20), (2, 30), (3, 40), (4, 50)]
((1, 'P'), (2, 'y'), (3, 't'), (4, 'h'), (5, 'o'), (6, 'n'))
[(1, 'name'), (2, 'age'), (3, 'city')]
[(1, 'John'), (2, 25), (3, 'London')]

❮ Python Built-in Functions

5