Better Python for Loops Using enumerate

November 19, 2020

It’s a common use case to iterate over a list together with its index. Traditionally, this has been done by using a for loop, utilizing range and len to create a list of indices. The items of the list then are accessed using [] notation, like so:

shopping_cart = ['apple', 'cereal', 'banana', 'cola']

for i in range(len(shopping_cart)):
    print(f'{i}: {shopping_cart[i]}')

# 0 apple
# 1 cereal
# 2 banana
# 3 cola

But this solution is very imperative, reminiscent of older programming languages. We have a better, more pythonic solution.

Using the enumerate keyword, we can get an iterator that already gives both the item and index of the list. Using this, we can simplify the above code as follows:

shopping_cart = ['apple', 'cereal', 'banana', 'cola']

for i, item in enumerate(shopping_cart):
	print(f'{i}: {item}')

# 0 apple
# 1 cereal
# 2 banana
# 3 cola

I personally think that this solution is cleaner, more elegant, and simpler to write.

Share on TwitterEdit on Github

Hi! I'm Adrian. I'm a software engineer, and I work hard to provide helpful and highly intuitive content for free. If you like what you read, please consider following me on Twitter. Hope to see you again next time!

All materials © Adrian Perea 2020