Methods and Functions

Python Enumerate

Python enumerate is an in-built function that adds a counter to an iterable and returns it in the form of an enumerate object. This enumerate python, can then by using the list() method be converted into a list of tuples or can be used directly in for loops. In this way you don’t have to increment the counter yourself.

python enumerate, python enumerate list

Enumerate Python Paramters

Syntax of enumerate in python

enumerate(iterable, start=0)

The enumerate in Python takes in two inputs as parameters, which are:

Iterable An object that supports iteration.
Start Index (optional) The index value(an integer) from which the counter starts, by default, is 0, but you can specify the start index value by using this optional parameter in enumerate.

Return value from enumerate python

The enumerate function python returns an iterable object, after adding count values to all the items in the iterator object, that is given as input.

Python Enumerate Example

Example1: How enumerate() works in Python?

Let us try an example without specifying the start index value and see what the output looks like.

                    

list1 = ["python", "java", "SQL"]

obj1 = enumerate(list1)

print(list(obj1)

The above code results in the following output:

                    

[(0,'Python'), (1,'Java), (2,'SQL')]

Since we did not specify the start index value, it by default started from 0. Here, we are using the list() function to display the output of the enumerated object.

Now, let’s try the enumerate() function by specifying the start index value.

                    

list1 = ["python", "java", "SQL"]

obj1 = enumerate(list1,2)

print(list(obj1)

The above code results in the following output:

                    

[(2,'Python'), (3,'Java), (4,SQL)]

Since we specified the start index value as 2 the count value for the first item started from 2 and incremented by 1 by default till the end of the loop.

Example 2: Python Enumerate for Loop

Let us try to loop over an enumerate object without specifying the start index value and see how the output looks like.

                    

list2 = ['A','B','C','D']

for i in enumerate(list2):
  print(i)
  print("\n")

The above code results in the following output:

                    

(0, 'A') (1, 'B') (2, 'C') (3, 'D') Since we did not specify the start index value, by default the count value started from 0. Now, let us try the same by specifying the start index value and see how the output looks like.

                    

list2 = ['A','B','C','D']

for i in enumerate(list2,5):
  print(i)
  print("\n")

The above code results in the following output: (5, ‘A’) (6, ‘B’) (7, ‘C’) (8, ‘D’) Since we have specified the start index value as 5 now, the count value started from 5 and got iterated by 1 by default until the end of the loop.                               

FAQs on Python Enumerate

Now, we have answered some frequently asked questions to give you a quick brief on how the enumerate() function works and how to use it.

Q1. What is Python enumerate?

A. Enumerate() in Python is an in-built function that exists in the Python library, which is used to count every item in the iterator object which will be given as input. So, while using the enumerate() function we should give an iterator object as input and a start index value which is optional to get the list of the iterator object with along with the count values. While using enumerate() function we also use list() function to display the output.

Q2. What is the use of enumerate () in Python?

A. In Python, the enumerate() function is used to return an iterator object with the count values. In simple terms, it is used to keep a count of the iterations until the loop ends.

Q3. How do you write enumerate in Python?

A. In Python, the enumerate() function can be written as:

                    

list = ['Ajay','Vijay','Sanjay'] 

enumerate(list1, start=0)

We give two paramters to the enumerate function(). A first parameter is an object that supports iteration and the second parameter(optional) is an integer to specify from the count value should start.

Q4. How do you enumerate 1 in Python? A. We can enumerate 1 in Python by using the range() function. Let us look at the syntax for how to do this.

                    

print(list(enumerate(range(1,2), 1)))

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.