Python enumerate()

The enumerate() in Python is a built-in function that adds a counter as a key to an iterable object (list, tuple, etc.) and returns an enumerating object.

enumerate() Syntax 

The syntax of enumerate() is:

enumerate(iterable, start=0)

enumerate() Parameters

The enumerate() function takes two parameters.

  • iterable – any object that supports iteration. E.g.:- list, tuple, etc.
  • start (optional) – the index value from which the counter needs to be started. If not specified, the default value is taken as 0

enumerate() Return Value

The enumerate() method adds a counter as a key to an iterable object (list, tuple, etc.) and returns an enumerating object.

The enumerated object can then be used directly for loops or converted into lists and tuples using the list() and tuple() method. 

Example 1: How enumerate() method works in Python?

# Python program to illustrate enumerate function

fruits = ['apple', 'orange', 'grapes','watermelon']
enumeratefruits = enumerate(fruits)

# check the type of object

# converting to list

# changing the default from 0 to 5
enumeratefruits = enumerate(fruits, 5)


<class 'enumerate'>
[(0, 'apple'), (1, 'orange'), (2, 'grapes'), (3, 'watermelon')]
[(5, 'apple'), (6, 'orange'), (7, 'grapes'), (8, 'watermelon')]

Example 2: Using Enumerate object in loops

# Python program to illustrate enumerate function in loops

lstitems = ["Ram","Processor","MotherBoard"]

# printing the tuples in object directly
for ele in enumerate(lstitems):
	print (ele)


# changing index and printing separately
for count,ele in enumerate(lstitems,100):
	print (count,ele)


#getting desired output from tuple
for count,ele in enumerate(lstitems):
    print(count, ele)


(0, 'Ram')
(1, 'Processor')
(2, 'MotherBoard')

100 Ram
101 Processor
102 MotherBoard

0 Ram
1 Processor
2 MotherBoard
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Importerror No Module Named Requests

ImportError: No module named requests

In Python, if you try to import Requests without installing the module using pip, you will get ImportError: no module named requests error.  In this tutorial, let’s look at installing…
View Post
Python String Rjust().Png

Python String rjust()

Table of Contents Hide rjust() Syntaxrjust() Parametersrjust() Return valueExample 1 : Python program to right justify a stringExample 2: rjust() Method With * fillcharExample 3: Returns original string if the…
View Post
How To Rename Columns In Pandas Dataframe

How to rename columns in Pandas DataFrame

Table of Contents Hide Method 1: Rename Specific column names in Pandas DataFrameMethod 2: Rename all column names in Pandas DataFrameMethod 3: Replace specific characters in Columns of Pandas DataFrame…
View Post
Python String Isprintable()

Python String isprintable()

Table of Contents Hide What are printable characters in Python?isprintable() Syntaxisprintable() Parametersisprintable() Return ValueExample 1: Working of isprintable() method Example 2: How to use isprintable() in real world? The Python…
View Post