Coursera Learner working on a presentation with Coursera logo and
Coursera Learner working on a presentation with Coursera logo and

In the Python program, programmers often use the for loop to create a loop on an iterable object. What this essentially means that you will not require a counting variable for accessing items inside the iterable. In certain cases, however, programmers require a variable capable of changing on every loop iteration. Instead of creating and increasing variables, one can always utilize the enumerate () function for getting a counter, along with a value simultaneously.

Iterating Using the Loops Present in Python

Python’s for loop utilizes collection based iteration, meaning that Python tends to assign an item to loop variables ( from iterables) in every single iteration. Here is an example.

  

The values in this example are a list that contain 3 strings, which happen to be the alphabets a, b and c. In the Python program, lists are considered as variant of iterable objects. The loop variable present in the for loop is value. It is also worth keeping in mind that on every loop iteration, value tends to be set towards the next item and it comes from the command values.

After this, you have to start printing value on the screen. One of the main advantages of collection based iterations provides is that it steers clear from off by one errors commonly found in plenty of other programming languages.

Let’s say that besides the value, you plan to print the item’s index on the screen and on every iteration. Well, an effective way of approaching this task would be to form a variable for storing the index. Once you do that, you can follow through by updating it on every iteration, which will look something like this:

In the example mentioned above, the index is essentially an integer keeping track of your position in the list. On every loop iteration, you will be printing index long with value. The final step in the loop will require you to update the number present in the index (make sure you update it by one). You will notice a bug whenever you fail to update the index for each iteration. This is how it could look like

In the example, you will notice that the every index on each iteration remains at 0. This is because their value has not been updated by a code at the loop’s end. Tracking down a bug like this can be incredibly challenging, especially in complicated and longer loops.

An effective way to tackle this problem is combining range() and len() for creating an index. The reason why this tactic is often recommended because it helps you create indexes automatically, ensuring you don’t have to remember when you have to make the update. 

Using enumerate() in Python the Right Way

Programmers can make use of enumerate() within a single loop just like they would use the initial iterable object. Rather than putting adding the iterable directly after the in present in for, you will be putting it in the enumerate() command’s parentheses. In addition, you will need to make slight changes to the loop variable. Here is an example that shows how this will look like:

After you typing in enumerate(), you will get 2 loop variables from the function, which will consist of the current iteration’s count along with the value of the current iteration’s item.

Similar to a regular for loop, you can give any name to the loop variables. In the example above, the words value and count were used. However, you could also name them v and i, or any other name as long as it is valid in Python. 

When using the enumerate () function, you do not have to remember when accessing the item in the iterable. What makes things even better is that you do not have to remember about advancing the index present in the loop’s end. Python takes care of almost everything automatically, ensuring you don’t have to worry about remembering essential details. 

Important Note

Using the 2 loop variables, which are value and count, while separating them with a comma showcases argument unpacking. This is an incredibly powerful feature in Python.

The enumerate() in Python has an added argument programmers can use for controlling the count’s starting value. The starting value happens to be zero by default. This is because Python’s sequences are indexed and all of them begin with 0. In simple words, whenever you plan to retrieve a list’s initial element, make sure to pick index 0.

Practicing enumerate() in Python the Right Way

Programmers can use the enumerate() function whenever they want to use the item and count within a loop. It is worth keeping in mind that using enumerate() essentially increases the overall count by 1 on each iteration. That said, this will not cause too much inflexibility. In fact, you can remain quite flexible as the count happens to be a regular Python integer, which you can utilize in more ways than one. 

The Internal Working of enumerate() in Python

To get a clearer sense of the way enumerate() actually works, it would be best to implement the version you have with Python. There will be two critical requirements in your enumerate() version. Here they are:

  • Begin by accepting starting count value and iterable as arguments
  • Send a tuple back, ensuring it has the present count value along with the item associated with the iterable

In the example above, my_enumerate uses 2 arguments, start and sequence, with zero being start’s default value. As far as the function definition goes, you have to use n as the value for start while running a for loop on sequence. For every elem present in sequence, make sure you yield control towards the respective location. After this step, send the present values of elem and n back. Finally, increment the n command to prepare for the upcoming iteration. 

 

If you want to enumerate in Python like an expert programmer, knowing the fundamentals is vital. The information and examples mentioned in this piece will give you a clear sense of direction about how to enumerate the right way.