Enumerate in Python

Enumerate in Python

4 mins read591 Views Comment
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

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
3 hours
Free
19 hours
– / –
4 months
Free
2 hours
Free
9 hours
3 K
3 weeks
– / –
40 hours
3 K
10 hours

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
Functions in Python
Functions in Python are very important content in any programming language since they are part of the universal programming structure. Functions in Python are very important content in any programming...read more
Introduction to Recursion Functions in Python
Introduction to Recursion Functions in Python
When programming, in many places you would have implemented a function that invokes or calls another function. However, when a function calls itself we call that recursion.
Python Map() Function
Python Map() Function
Python is an object-oriented programming language that also offers several functional programming utilities. In this article, we will focus on one of the most special in-built functions it offers –...read more

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
Lambda Function in Python
Python allows to create small anonymous function using lambda expression. In this article, we will learn how to work with lambda function in python.
Python Join() Function
Python Join() Function
During Python programming, you might encounter instances where you would need to create strings from iterable objects. For example, building a sentence from a sequence of words. For this, the...read more
Python Sorted() Function
Python Sorted() Function
The sorted() function is designed to return a new sorted list from the items in an iterable. Whether you're dealing with numbers, strings, or custom objects, sorted() can handle it...read more

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
Introduction To Decorators In Python
In this article, we will discuss in detail about what are python decorator, why we need them and finally how to create python decorators.
Tutorial – for Loop in Python
Tutorial – for Loop in Python
Let’s read about for Loop in Python in the below tutorial.
Classes and Objects in Python
Classes and Objects in Python
This article includes classes and objects in python. This is very important topic from interview point of view.

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
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due to...read more
Pandas Interview Questions for Data Scientists
Top 9 NumPy Interview Questions
Top 9 NumPy Interview Questions
In this article, we will discuss basic and intermediate NumPy Interview Questions in most simplified ways with example.
About the Author
qna

Comments