Introduction to Python Enumerate
Many a time, when we work with iterators, we may get a need to count the number of iterations as well. This can be taken care of in multiple ways, but fortunately, python provides us with an inbuilt functionality known as enumerate() function for taking care of this task. In a nutshell, enumerate function() adds a counter to an iterable object and then returns it back as an enumerate object.
This enumerate object returned then can be utilized in a direct fashion either in loops, or the enumerated object can then be converted into a list of tuples and utilized accordingly.
Syntax of enumerate() function is:
Several arguments are passed as well with enumerate function:
- iterateable_object: an object that can be iterated upon, for instance, list, tuple, etc.
- start=0: It’s the index value from where the counter will initiate. By default, the counter will start from 0
Example of Python Enumerate
Let’s take an example to understand this in detail. Python program to showcase the functionality of enumerate function
list = [ " Learn " , " Data " , " science " , " at ", " EDU " , " CBA" ] str = "EDUCBA" # Here we are creating the enumerate objects obj1 = enumerate( list ) obj2 = enumerate( str ) print ("Return type of object 1 is:",type(obj1) ) print (list(enumerate(list))) # here we are changing the start index to 2 from th default option that is 0 print (list(enumerate(str,2)) )
- We have initialized a list and a string, both being iterated able objects that can be utilized as an argument to enumerate function
- Then we have defined enumerate function objects. obj1 to count the iterations for the iterate able list & obj2 to count the iterations for the objects in the list.
- obj1 = enumerate( list )
- obj2 = enumerate( str )
- Thereafter the print function is used to print the enumerated object returned by the enumerate function.
- The first print statement identifies the return type of the enumerate function, and we can see in the output that it’s an enumerate object.
- print (“Return type of object 1 is:”,type(obj1) )
- Return type: < type ‘enumerate’ >
- The second and third print statement is utilized with the list and string respectively.
Using Enumerate Objects in Loops
What if we want to use Enumerate Objects in Loops. Let’s take an example to understand the same.
Python program to understand the usage of enumerate function in loops
list = [ " Learn " , " Data " , " science " , " at ", " EDU " , " CBA" ] # printing the tuples in object directly for element in enumerate(list): print (element ) # changing index and printing separately for count,element in enumerate(list,100): print (count,element)
- We have initialized an iterate able object at the start of this program named “list.”
- For loop is utilized to print each element in this list
- In the second loop, the start is initialized from 100, which by default is 0 and is used to print the count and the element of the iterate able object list as well
Creating a Python Dictionary Using a Python List and Enumerate Function
Let’s take an example to understand this in detail.
List = [ " Learn " , " Data " , " science " , " at ", " EDU " , " CBA" ] Dictionary = dict(enumerate(List)) print(Dictionary ) 2
How Does it Work?
- We have defined a list in the staring of the program
- Thereafter we have created an enumerate object using the list and converted it into a dict, further saving it into the “Dictionary” variable
- Finally, we are printing the object named “Dictionary”.
How Can We Enumerate from a Specific Index?
Let’s take an example to understand this in detail
Python program to understand on how to enumerate a string
String = 'EDUCBA'
for i, j in enumerate(String, 1):
This is a guide to Python Enumerate. Here we discuss the Introduction to Python Enumerate along with different examples and code implementation. You may also have a look at the following articles to learn more –