Enumerate in Python
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.
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
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).
Syntax
enumerate(iterable_object, startIndex)
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.
Now, using the above syntax, let’s write the code for the above example.
Input
# Make a list of studentsstudent = [‘Rahul’, ‘Ram’, ‘Siddiqui’, ‘Nikita’, ‘Sam’, ‘Saumya’, ‘Ritika’, ‘Ritvik’, ‘Twinkle’, ‘Aquib’]
#defining enumerate functionenumerate_student = enumerate(student, 1)
#printing enumerate objectsprint(list(enumerate_student))
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))
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)
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)
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)
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.
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.
Conclusion
In this article, we have briefly discussed enumerate () function in python with examples.
Hope, you will like the article.
Keep Learning!!
Keep Sharing!!
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
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