Enumerate in Python

Enumerate in Python

4 mins read591 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 13, 2023 14:03 IST

Enumerate is a built-in function of python, that helps to count each item of a list, tuple, or any data structure that can be iterated. In this article, we will discuss enumerate function in python with examples.

2022_10_MicrosoftTeams-image-52.jpg

Enumerate means defining the list of things in terms of numbers. In python, enumerate function takes data as a parameter and returns an enumerate object. It returns the object in key-value pair, where the key is the corresponding index of each item, and the value is the given item. This article will discuss what is enumerate in python and how to use it.

Explore How to Find an Armstrong Number Using Python

Table of Content

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

What is Enumerate in Python?

Enumerate in a built-in function of Python that helps you count each object of a list, tuple, set, or any data structure that can be iterated.

  • It takes input as a collection of tuples and returns it as an enumerate object.
  • An enumerated object (returned as an output) can be used directly for loops 
    • It can be converted as a list of tuples using the list method.

Example: If there are ten students enrolled in any program, you have to assign them a roll number on the basis of their admission.

So, basically, you have to assign a number from 1 to 10 (1 for the first student and 10 for the last student). 

Functions in Python
Introduction to Recursion Functions in Python
Python Map() Function

Syntax

 
enumerate(iterable_object, startIndex)
Copy code

Let’s discuss the parameters in the syntax of enumerate function.

  • iterable_object: 
    • Objects whose enumeration is to be done. 
    • It can be a string, list, or any data structure.
    • In simple terms, it is an object that can be looped.
  • startIndex:
    • It is optional.
    • It is the index value from which the counter is to be a start
    • If startIndex is not specified, it will be a default 0.
What is Programming What is Python
What is Data Science What is Machine Learning

Now, using the above syntax, let’s write the code for the above example.

Input

 
# Make a list of students
student = [‘Rahul’, ‘Ram’, ‘Siddiqui’, ‘Nikita’, ‘Sam’, ‘Saumya’, ‘Ritika’, ‘Ritvik’, ‘Twinkle’, ‘Aquib’]
#defining enumerate function
enumerate_student = enumerate(student, 1)
#printing enumerate objects
print(list(enumerate_student))
Copy code
Lambda Function in Python
Python Join() Function
Python Sorted() Function

Working of Enumerate Function in Python

  • It is like a for loop.
    • It loops over a collection of each item.
  • It assigns a particular index to every item that can be used to refer to that item.
  • As functions iterate over the object, it turns each item into a key-value pair.
  • Starting index parameters in an enumerate function accepts only an integer, and it is very similar to the indexing of arrays.
Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Now, let’s take some more examples to get a better understanding of how to enumerate function works with a list, tuples, set, or dictionary.

Examples:

Python List

Code

 
car_list = [‘Honda’, ‘Tata’, ‘Mahindra’, ‘Ford’, ‘Toyota’]
e_car_list = enumerate(car_list)
print(list(e_car_list))
Copy code

Output

[(0, ‘Honda’), (1, ‘Tata’), (2, ‘Mahindra’), (3, ‘Ford’), (4, ‘Toyota’)]

Also Read: Complete Guide to List in Python

Python Tuple

Code

 
car_tuple = (‘Honda’, ‘Tata’, ‘Mahindra’, ‘Ford’, ‘Toyota’)
for i in enumerate(car_tuple):
print(i)
Copy code

Output

(0, ‘Honda’)

(1, ‘Tata’)

(2, ‘Mahindra’)

(3, ‘Ford’)

(4, ‘Toyota’)

Also Read: Understanding Tuples in Python

Also Read: Python List vs Tuple

Python String

Code

 
my_string = “NaukriLearning”
for i in enumerate(my_string, 5):
print(i)
Copy code

Output

(5, ‘N’)

(6, ‘a’)

(7, ‘u’)

(8, ‘k’)

(9, ‘r’)

(10, ‘i’)

(11, ‘L’)

(12, ‘e’)

(13, ‘a’)

(14, ‘r’)

(15, ‘n’)

(16, ‘i’)

(17, ‘n’)

(18, ‘g’)

Also Read: Getting started with Python

Also Read: Understanding Python Sets

Python Dictionary

Code

 
my_dict = {‘a’: ‘A’, ‘b’: ‘B’, ‘c’: ‘C’, ‘d’: ‘D’}
for i in enumerate(my_dict):
print(i)
Copy code

Output

(0, ‘a’)

(1, ‘b’)

(2, ‘c’)

(3, ‘d’)

Also Read: Introduction to Python Dictionary

Also Read: Python Dictionary Practice Programs

Return value of enumerate () function in Python

Using the enumerate () function, we convert a list, tuple, or data structure to an enumerated object. But it can again be converted into other iterable data structures like lists and tuples using the list () and tuple () methods, respectively.

Introduction To Decorators In Python
Tutorial – for Loop in Python
Classes and Objects in Python

Advantages and disadvantages of Enumerate function:

  • It loops through a list, tuple, dictionary, or any data structure and returns indexes for corresponding values.
  • It can be used at the runtime of the loop.
  • It has the flexibility to start the index from any integer.
  • It avoids the use of a loop and thus makes code cleaner (a little bit).
  • Uses more space complexity than a normal list.
  • It only counts in a consecutive way
    • i.e., if we start from any number I and then an increment by +1 only.
    • If you want to change the step size from 1 to 3 or 5, then we have to use a loop.
Top 110+ Python Interview Questions and Answers
Pandas Interview Questions for Data Scientists
Top 9 NumPy Interview Questions
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio