What is Enumerate in Python?

The enumerate function takes an iterable like a list, tuple etc. and returns it as an enumerate object. The function adds counter to the iterable. For example:

FruitEnu = enumerate(FruitsLst)

The general way of using the enumerate function is:

enumerate(iterable, start=0)

  • The iterable can be an iterator, sequence or an object that supports iteration.
  • The default value of the optional argument start is 0. It defines the starting number of the enumerable object.

An example of enumerate function with list

In this example, we have a list of fruits. Then I used the enumerate function and assigned its return value to a variable.

Finally, the list function is used to display the content of that object variable:

#Enumerate function demo

FruitsLst = ['Apple', 'Banana', 'Strawberry', 'Orange']



FruitEnu = enumerate(FruitsLst)



# Display counter with elements

print(list(FruitEnu))

The output:

[(0, ‘Apple’), (1, ‘Banana’), (2, ‘Strawberry’), (3, ‘Orange’)]

You can see, it displayed list of tuple with counter and list elements. As start argument was not provided, so the counter value started at 0.

The next example shows starting the counter value from 10.

Using the start argument in enumerate function

The example below uses the second argument as well in the enumerate function. As mentioned earlier, this is the start value (a number) that defaults to zero. In this example, I used 10:

#Enumerate function demo with start argument

FruitsLst = ['Orange', 'Pine Apple', 'Apricot', 'Avocado', 'Cantaloupe']



FruitEnu = enumerate(FruitsLst,10)



# Display counter with elements from 10

print(list(FruitEnu))

The result:

[(10, ‘Orange’), (11, ‘Pine Apple’), (12, ‘Apricot’), (13, ‘Avocado’), (14, ‘Cantaloupe’)]

So, what is the usefulness of enumerate function?

  • It enables us to loop over something
  • An automatic counter is added
  • The ability to change the start number makes it even more useful.
  • By using list function, you may create a tuple of index and list items.

A few more examples of the enumerate function are given below.

Knowing the return type example

By using the type function, you may get the type of returned object after using the enumerate function:

FruitsLst = ['Cherimoya', 'Cherries', 'Clementine']



FruitEnu = enumerate(FruitsLst,15)



# Display the type

print(type(FruitEnu))

The output:

<class ‘enumerate’>

Using enumerate function in for loop example

In this example, we have an enumerate object and iterated it by using a Python for loop. The counter and list elements are displayed as follows:

LstNum = [5,7,9,11,13,15]

for x, cur_num in enumerate(LstNum):

    print (x, cur_num)

The output:

Python enumurate

Iterating with different start number example

The start argument is also given in this example:

#Enumerate object iteration



FruitsLst = ['Orange', 'Pine Apple', 'Apricot', 'Avocado', 'Cantaloupe']

for x,fruit in enumerate(FruitsLst,50):

    print (x, fruit)

Python enumurate iterate

Enumerating a tuple example

In this example, the enumerate function is used with the tuple. Again, a for loop is used to display the tuple items and count/index:

#Enumerate with tuple



NameTup = ('Mike', 'Saleem', 'Benjaman', 'Marie', 'Sheela')

for count_in,name in enumerate(NameTup):

    print (count_in, name)

The output:

0 Mike

1 Saleem

2 Benjaman

3 Marie

4 Sheela

Using enumerate function with a string

You may also use enumerate function with the string and get the count added. See an example below:

#Enumerate with String



str_x = "Python"

for count_in,st in enumerate(str_x):

    print (count_in,"\t",  st)

The output:

0               P

1               y

2               t

3               h

4               o

5               n

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!