Programs

Enumerate() Function in Python : A Basic Walkthrough

Python is one of the most preferred languages among data scientists. The language is intuitive, and python has an array of useful libraries to tackle different problems. It has numerous modules to help data scientists across various stages of any data science problem – from data mining to analysis to reporting. 

At upGrad, we’ve mentored professionals from diverse fields, including economics, humanities, engineering. Python has by far been the easiest language to learn both for beginners and professionals at any stage of their career. So, if you’re an aspiring data scientist looking to take your first steps, Python could be a good starting point! 

The Python programming language has multiple features to help you sift through different datasets and manipulate data containers as per your requirement. One of such features is the enumerate() method. It is one of the more important functions in Python, especially for working with voluminous datasets. Not only that, but the function is also a lot more optimized than other Python functions that perform a similar task – it keeps your code efficient and clean. 

The function adds a counter to any iterable object. Then, using the counter, you can keep track of the number of iterations that have occurred. The value of this counter is stored separately. Using this method, you can loop over a list or dictionary or tuple or set of items while keeping track of the index of all the elements. 

In this article, we’ll be breaking down the enumerate() function further!

Syntax and Application of the enumerate() Method

The enumerate() function creates a separate counter variable that keeps track of the number of iterations in a loop. It is a built-in Python function, so you don’t need to import any libraries to access this.

While using the enumerate() function, you don’t have to worry about creating the range() statement to get the array’s complete length. Since you have the counter variable, you can quickly iterate over enumerate() without creating a different iterator. 

Here’s the syntax of the enumerate() method: 

enumerate(iterable, startIndex)

It has two parameters: 

  • Iterable: any object that can be looped over. 
  • startIndex: This is optional. If you give any value here, the count will start from the value you provide, and it will increment till the end of the loop. By default, the startIndex value is 0. 

Let’s understand the working of the enumerate() method by checking a few examples! 

Here is a little piece of code. We have an array by the name of food_items, and, in the for loop, we’ve called the enumerate() method with the iterable being food_items (this will be the array that this function will loop over). Then, we’re trying to print out the index and name — these are the two elements returned by the enumerate() 

food_items = [“Pizza”, “Burger”, “Salad”, “Lasagna”, “Spaghetti”]

for index, name in enumerate(food_items):

   print(index, name)

The enumerate() function iterates over our food array item and returns index and name. The index is the number of iterations that have happened, and the name is the particular item in the list. Here is what the program outputs:

0 Pizza

1 Burger

2 Salad

3 Lasagna

4 Spaghetti

As you can see, the output is as you would have expected – you get an object of indexes and names for the different items in your food_items list. Using the enumerate() function instead of len() or range() makes our code extremely clean and readable! 

Working with the Enumerate Object

By default, enumerate() returns a list of tuples. We can see this by printing the value of ‘item’ while iterating over the food_items array: 

for item in enumerate(food_items):

   print(item)

This code returns the index value and the food item at that index in a tuple data structure.  

(0, ‘Pizza’)

(1, ‘Burger’)

(2, ‘Salad’)

(3, ‘Lasagna’)

(4, ‘Spaghetti’)

If you’ve paid attention since the beginning of this article, you’d recall that the enumerate() method takes in two parameters – iterable and startIndex. By default, our method so far was talking – as the startIndex. Let’s try to change that and see how it works out! 

food_items = [“Pizza”, “Burger”, “Salad”, “Lasagna”, “Spaghetti”]

for index, name in enumerate(food_items, 10):

   print(index, name)

Our program returns the following

10 Pizza

11 Burger

12 Salad

13 Lasagna

14 Spaghetti

This is useful for use cases where you’d want to start your list from a number other than 0. 

We can also create tuples with the list item and index using the enumerate() method. Here’s an example: 

food_items = [“Pizza”, “Burger”, “Salad”, “Lasagna”, “Spaghetti”]

food_items_with_counter = list(enumerate(food_items, 1))

print(food_items_list_with_counter)

The code will return a list of tuples with the food items and the index value from the enumerate() method. Check it out: 

[(1, ‘Pizza’), (2, ‘Burger’),(3, ‘Salad’), (4, ‘Lasagna’), (5, ‘Spaghetti’)]

Conclusion

The enumerate() function in Python is a robust and optimized function. It is useful if you want to iterate over an array of values and manipulate data at different locations of the array. 

Now that you know the basics of enumerate(), go on and practice – create a couple of arrays and try running the enumerate() function with different inputs. After all, programming is all about practice. It’s important to add – programming (and Data Science, particularly) is also about knowing the right things, apart from just blatantly practising. You need to find a mix of the two – knowledge and practice. 

If you’re a fresher in this field looking for help – upGrad has your back! Our Master of Science in Machine Learning and Artificial Intelligence runs in collaboration with IIIT-Bangalore and LJMU. You get 1-on-1 support from industry experts, a strong peer group of students, and 360-degree career support. 

Reach out to us today and advance your career!

Lead the AI Driven Technological Revolution

MASTERS IN MACHINE LEARNING AND ARTIFICIAL INTELLIGENCE
Apply Now

0 replies on “Enumerate() Function in Python : A Basic Walkthrough”

Accelerate Your Career with upGrad

Our Popular Machine Learning Course

×