The Enumerate Method in Python

May 6, 2021

I'm just starting my journey into Python 3.8 from C++98. It's a long way to go, and as I learn more, I realize just how far behind the times I am. Nevertheless, it's incredibly rewarding. I find so many examples of how a programming language created in the last decade solves problems that I run into on a daily basis at work.

A really simple example of this is the enumerate method. Most times I see this method being used is in examples which are nearly impossible to understand as a new Pythonista. These examples (while showing how incredibly powerful the language is) makes the enumerate keyword seem nearly unusable until having a strong command of the language.

In reality, I might be able to use the enumerate method every single day (though maybe that just comes because of my heavy C++ accent as I slowly get used to Python). Here's an example:

1
2
3
4
5
6
7
8
9
names = [
    'Abigail', 
    'Betty', 
    'Carlos',
    'Dan'
]

for ii in range(len(names)):
    print(f'The name at position {ii} is {names[ii]}')

In C++, this is just the way things are done. However, Python's enumerate method makes this loop a little more simple and more Pythonic:

1
2
for pos,name in enumerate(names):
    print(f'The name at position {pos} is {name}')

You might even be able to do that last for loop as a one-liner, but I'm not clever enough to figure that one out.

What the enumerate method does is return each index of the iterable as an index, value pair represented by a tuple. For instance, if I were to enumerate the names list, I would get:

>>> print(list(enumerate(names)))
[(0, 'Abigail'), (1, 'Betty'), (2, 'Carlos'), (3, 'Dan')]

Instead of having to maintain your own counter, the enumerate method maintains it for you. So you'll no longer have to use the ugly 'i', 'ii', 'idx', 'count' variable which is so prevalent in C++. Instead, you can use descriptive variable names, and you'll never again fall victim to the easy mistake of forgetting to increment your counter and ending up with an infinite loop.

Anyways, since this just clicked for me yesterday, I thought I would share it. I hope I can make it click for another new Python user. If you have any good examples of how you use the enumerate method, feel free to shoot me an email.