Blog

Python enumerate with Example

  • (5.0)

Introduction:

Python is a powerful programming language that has started regaining its fame for its usage in the Data Science along with the latest technologies like R and etc. Having said that, let us take a look at the tiny winy bits of concepts to get ourselves stronger in this programming language.
In this article, we will try to understand the concept of enumerate() in Python programming language. To provide you a better understanding on what enumerate() can be used for in Python programming language, take a look at the following section.

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

Python enumerate():

In the Python programming language there is a built-in function that is provided - enumerate(), that is used to traverse / iterate through a list keeping a track on the indexes of the list items. Most of the newbies along with the professionals do not understand the usefulness of the provided function. This built-in function was provided with a generic notion of usage in the world of lists and arrays when used with numerous elements inside it. This not only remembers the index at which an element is present, provides ways and means to modify the indices for presentation purposes as well.
Let us take a small example and see how this is made possible in Python.

To understand the example above, there were three elements in the list with indices 0, 1 and 2. Now with the enumerate() function, we are able to traverse through the list and identify the elements along with the indices of the elements that they are present in the list. The for loop traverses for every match that is been provided by the enumerate() method and uses the data to print out the same on to the Python console. The output to the program shown above clarifies the concept of enumerate() function without a doubt.

Frequently Asked Python Interview Questions & Answers

We can modify the above code, to show the same output with modified indices to these elements present in the list. That can be achieved via an additional parameter to the enumerate() function itself. Let us take a look at the modified example to clarify this concept:

This is generally used to show the output with a modified index instead of the common notation of indices of a list which starts by 0 (by default).

Related Page: Python Enum

Now let us counter the same example with a modified way of achieving it, without using the additional argument that we can pass to the enumerate() function. This is a simple logic, but lets you know how to mend and bend based on your requirements to code in Python programming language.
Let us take a look at the modified way of doing the same in a different manner:

Let us now look at the same example which will be modified to create tuples along with their indices to be shown as output. The representation of the list containing the elements is now elegantly modified to be represented as tuples with indices accompanied to the element itself. This is pretty easy with Python, not much to code as well. Let us take a look at the modified code and how this transforms the output totally:

 

Conclusion:

In this article, we have seen what cases enumerate() can be used and why are they used in Python programming language. We have also tried to take a deeper look into the concept with various examples as well.
Hope that you were clear with the concepts after going through this detailed article, please do comment if you have any suggestions to make.

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules