Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Enumerate() in Python

Enumerate in Python :

Sometimes we need to keep count of iterators or the index position of an sequence  so that we can use the index of the sequence later in the code as per need.

Python provides an in-built function  to do this operation – Enumerate().

Enumerate() function adds a counter to an iterable and returns it in a form of enumerate object. This can be directly used or can be converted into a list of tuples.

enumerate in python

Syntax : 


  iterable - any object that is iterable 

start(optional) - from index value to start, default value is 0

Return Value from enumerate()

enumerate() function adds counter to an iterable and returns it. The returned object is a enumerate object.

You can convert enumerate objects to list of tuple using list().

Code #1 :

#pyhton program 


Output :

<enumerate object at 0x2b4f5a14e090>
[(0, 'P'), (1, 'r'), (2, 'e'), (3, 'p'), (4, 'I'), (5, 'n'), (6, 's'), (7, 't'), (8, 'a')]

The above code will print the type of object enumerate returns and the second print statement will print the list of tuple of the object and we can see that start parameter is optional.

Every char of srting ‘b’ is enumerated with their index value.

Code #2:

#pyhton program 


for ind,char in enlist:

Output :

[[(2, 'P'), (3, 'r'), (4, 'e'), (5, 'p'), (6, 'I'), (7, 'n'), (8, 's'), (9, 't'), (10, 'a')]
0 P
1 r
2 e
3 p
4 I
5 n
6 s
7 t
8 a

In the above program start(parameter) value is 2 so counter starts from 2 instead of 0. we can print the tuple value seprately also.