You are currently viewing Python enumerate Explained with Examples

The Python enumerate() function allows iterable objects to return an enumerate object in the form of a tuple containing the index and the value. In this tutorial, you will learn what a Python enumerate() function is and how you can use it in your code to iterate through iterable objects.

Advertisements

In Python, there are several ways to iterate through objects such as lists, tuples, strings, and more. While using loops (such as while and for loops) to iterate through these objects is acceptable, it may not always be the most efficient in terms of performance. As a programmer, possessing the skill to find efficient solutions to problems is crucial. You may be wondering what the best alternative is for iterating through iterable objects in Python. The answer is the enumerate() function.

2. What is enumerate() function and why is it useful?

Before we start writing code about this function let us understand what it is. The enumerate() function is a Python built-in function that is used for looping over iterable objects such as lists, tuples, strings, etc. The enumerate() function returns the index and value of each element or item in the iterable object.

3. Syntax for enumerate() function 

To make the most out of this function, you need to understand its syntax. So here it is below:


# Syntax of enumerate()
enumerate(iterable, start=0)
ArgumentMeaning
IterableThis is the object to be enumerated e.g list, tuple, string
start(optional)This is the index value where the enumeration starts, the default value is 0 and it can be changed to any integer value
Python enumerate arguments

4. Basic usage of enumerate() function 

Now that you know the Python enumerate() function syntax, let us look at some basic enumerate() function examples. We will loop over a list of items like this:


# Usage of enumerate()
languages = ['Python', 'C++', 'Java', 'Ruby'

for index, language in enumerate(languages):
    print(index, language)

In this code, we create a list called languages that contains four programming languages. We then use the enumerate() function to loop through the list. enumerate() returns a tuple that contains the index and value of each element, and we use the variable’s index and language to assign these values respectively.

Within the loop, we print the index and value of each element using the print() function. The output of this code will be:


# Output:
0 Python
1 C++
2 Java
3 Ruby

6. Advanced usage of Python enumerate() function

If you can recall the Python enumerate() function syntax, we have start as an optional argument whose default value is 0. Now let us see how we can leverage this argument in our code:


# enumerate() using start argument
languages = ['Python', 'C++', 'Java', 'Ruby']

for index, language in enumerate(languages, start = 1):
    print(index, language)

This code snippet is the same as the first code snippet, but the only difference here is that the enumerate() function is taking the second argument start with a value of 1. This argument specifies where the index should start, in our case, it is starting at 1. If start is not specified, the index value defaults to 0.

The output for the above code will be:


# Output:
1 Python
2 C++
3 Java
4 Ruby

You can also change the start value to a bigger integer like in the below code:


# enumerate() with start param different value
languages = ['Python', 'C++', 'Java', 'Ruby']

for index, language in enumerate(languages, start = 100):
    print(index, language)

And the output will be:


# Output:
100 Python
101 C++
102 Java
103 Ruby

7. The enumerate() function vs loops(while and for loops)

Now you must be wondering about whether Python enumerate() function or loops are the best option. In this section, I will show you how these two compare:

  • Performance: When it comes to performance the enumerate() function is faster than loops
  •  Simplicity: Keeping track of the index value is easy using the enumerate() function than loops which require more lines of code to achieve the same
  • Readability: The enumerate() function provides a more concise and easy-to-understand way of looping through iterable objects than loops which are a bit more complex

With the help of the above comparisons, you now know which is the best option for looping over iterables in your code.

Conclusion

That concludes our tutorial on Python enumerate() function. I hope you found it useful and informative and that the knowledge gained will be used in your future Python languages. The Python enumerate() function basically allows iterable objects to return an enumerate object in the form of a tuple containing the index and the value