An Introduction to Enumerate in Python with Syntax and Examples

Usually, when someone iterates through an iterable using the conventional loops (for and while), at times it becomes cumbersome to keep track of the count and the relative positions of the elements in the iterable. When using for loops, there is no need to assign a variable to keep the count of the elements; however, there is sometimes a need for a variable that changes inside the loop based on some conditions. 

It is common to use the enumerate method in Python to map the counter’s value along with the iterable items. It can serve both as a counter and as a way to maintain the indices of the elements in the iterable. 

What is Enumerate in Python?

In simpler words, the enumerate function takes as input, an iterable and adds a counter to each  iterable element, and returns an enumerate object. The counter can also act as indices to each element which can be used to reference these elements at a later stage when required. 

Syntax of Enumerate in Python

The syntax of the in-built enumerate function is  - 

enumerate(iterable, start_index)

Parameters - 

  • Iterable: An iterable is an object that can be looped over. Examples of iterables in Python are tuples, lists, sets, etc.
  • Start_Index: This parameter is optional and defines the value from which the count should start for the first element in the loop. This count is then incremented by 1 for the succeeding elements, till the end. If this parameter is not defined, then the default value is 0.

Return Value - 

The return value of the enumerate function is the enumerate object which contains tuples with the count value and the item or the element of the iterables.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

For Loops Vs. Enumerate in Python

Example 1.

Let’s consider a simple example where loops are used to print the index along with the items of a list.

Program - 

letters = ["a", "b", "c", "d", "e", "f", "g"]

index = 0

for letter in letters:

   print(index, letter)

   index += 1

Output - 

LoopsVs.Enumerate_exp1

Here, you saw the use of a variable called index, to keep track of the count of the elements along with the index of each element in the iterable. Try another method to do the same.

Example 2.

You can also use range along with the length function to perform the same function.

Program - 

letters = ["a", "b", "c", "d", "e", "f", "g"]

for index in range(len(letters)):

   print(index, letters[index])

Output -

LoopsVs.Enumerate_exp2

This method is also inefficient since you have to use the length function to iterate over the iterables to find the count.

Example 3. 

Now, use the enumerate function to carry out the same operation. The enumerate function saves the memory of using an extra variable, and the returned value is a simple reference to the memory, and not an actual list or iterable of items.

Program - 

letters = ["a", "b", "c", "d", "e", "f", "g"]

for index, value in enumerate(letters):

   print(index, value)

Output - 

LoopsVs.Enumerate_exp3

It is not necessary to keep track of a separate count variable, advance it in each iteration, or go through the hassles of maintaining indices separately. Everything is handled by the enumerate function.

If you return the type of the enumerated object, you will find that it’s just a reference to a memory location.

letters = ["a", "b", "c", "d", "e", "f", "g"]

print(enumerate(letters))

Output - 

LoopsVs.Enumerate_exp3_1

Playing with the Start Parameter

You can mention a different starting value in the start parameter. The default is 0, in case you mention a different value, the counting starts from that value and keeps on incrementing for all the items in the iterable.

Program - 

letters = ["a", "b", "c", "d", "e", "f", "g"]

enumerated_list = list(enumerate(letters, 20))

print(enumerated_list)

Output - 

StartParameter

As you can see, the starting index has now changed to the one specified in the second parameter.

Looping over Enumerate Object in Python

In this example, try to loop over an enumerate object using a simple for loop. 

Program - 

letters = ["a", "b", "c", "d", "e", "f", "g"]

enumerated_object = enumerate(letters)

for i in enumerated_object:

   print(i)

Output - 

Looping_1

You can see that each object is a tuple inside the enumerated object that is returned.

Useful Tips for Using Enumerate in Python

  • After enumerating an iterable, the enumerate function returns an enumerate object by default. You cannot perform indexing on the enumerated object and get specific tuples. You can use the list() or tuple() method to convert the enumerated object into a list or tuple and start accessing specific elements.
  • Using traditional ‘for loops’ is messy and makes the code look dirty. Instead, you can always use enumerate to make the code cleaner and save on memory.

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Advantages of Using Enumerate in Python

  • You can use enumerate on any iterable, be it lists, sets, dictionaries, strings, objects, etc. 
  • Enumerate keeps your code clean by performing the same function with fewer lines of code.
  • You can keep track of the count of elements along with the index of each element through enumerating without having to maintain separate variables for each of them and incrementing the counter.
  • It’s an automatic counter and can be used to change the starting value of the index as well, by specifying the appropriate value in the second parameter.
Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Conclusion

In this guide, you have seen one of the best alternatives to the messy for loops in Python. You saw a basic introduction of the enumerate method in Python, along with the syntax and parameters that it accepts. This tutorial also explored the iterables that it takes as input, played with the start_index parameter, and saw a bunch of examples demonstrating how you can use enumerate instead of conventional for loops in python. 

Lastly, you learned some useful tips along with some advantages of using this method over loops.

We certainly hope that with the help of this guide, you will get hands-on experience working with enumerating in Python and be able to adopt it without a second thought. Have any questions for us? Leave them in the comments section, and our experts will get back to you on the same, as soon as possible!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.