Search

Application Of The Built-in Python Enumerate Function

python enumerate function
 

How many times have you ever wanted to loop through a list or tuple while keeping count of the number of times and you ended up doing it with a for-loop? For beginners, I think the answer would be many times. But that is not pythonic. For example, I notice this code often among many python programmers who are not aware of the existence of the built-in python enumerate function. They keep an antipattern consisting of a range over the length of a list while keeping a running total of how many times they have gone through the range object created.


fruits = ['mango', 'pawpaw', 'lettuce', 'orange', 'banana']
for i in range(len(fruits)):
    print(i, fruits[i])

Please, if you are doing this, it is harmful to your code and not pythonic because your code is not easy to read and is vulnerable to your making typing errors.

To prevent code like this, python has a nice function that does it elegantly called the python enumerate function.

The python enumerate function

The syntax of the python enumerate function is enumerate(iterable, start=0). This is a function that accepts an iterable as first positional argument with a start value for the counter to the iterable. The default for the start value is 0 but you can specify a starting value of your choice. When you enumerate an iterable, it gives you an enumerate object. The benefit of such an enumerate object is that it is an iterator with which you can use a for loop to return a count of the items in the enumerate object. We will get to that later. But let’s see how it works with an example of enumerate.

If you run the above code, you will find that the python enumerate function gives an enumerate object. Now, let’s loop over the enumerate object.

In the above code, I used the default start of 0 and you can see that when the counter was printed, the counter started from 0. We can tweak that feature to get a starting counter of our choice. Now for some examples of enumerate function.

So, you can see how powerful this little known feature in the python programming language is. The enumerate function gives all iterables an advantage the python dictionaries already have, which is an index notation that is compact and reliable.

So, the next question is: what can we do with the python enumerate function?

The simple answer is a lot.

Application of the python enumerate function

  1. Add a counter to a list or iterable
  2. .

    Just as in the example I gave above, there are lots of times we want to add a counter to a list or tuple, especially if it is a large list or tuple. This handy function makes it possible. You can use the counter as a key to get an item in the iterator that you want to use. For example, you have an extremely long list of cars and you want to be able to know them by number. You could use enumerate to give them a count value starting from any number and then retrieve each car based on the counter.

    For some example:

    What if we wanted to know the first, or third car?

  3. Convert to a list or tuple
  4. .

    We could convert the python enumerate object which is an iterator to a list or a tuple and use the handy functions of python lists or tuples. It is so easy to do. Just use the enumerate object as the argument to the list or tuple and it is easily done. When you have the enumerate object as a list, you can then use python list functions on them instantly.

    Here are some code examples.

    You can see from the above that I used the index of the cars_list list to get the last item in the list of cars, and then used the len function of the list to find out the number of cars in the list.

You can read about the rationale for the enumerate function from PEP 279 and its documentation at python.org.

No comments:

Post a Comment

Your comments here!

Matched content