DEV Community

Kelvin Wangonya
Kelvin Wangonya

Posted on • Originally published at wangonya.com on

When to use python's enumerate() instead of range() in loops

The range() function is often useful when iterating over a set of integers:

for n in range(50):
    ...

#

for n in range(10, 30):
    ...
Enter fullscreen mode Exit fullscreen mode

or a list of strings:

for fruit in ["apple", "mango", "banana"]:
    ...
Enter fullscreen mode Exit fullscreen mode

Now, say you want to iterate over the list of fruits and also show the index of the current item in the list. Using range(), this might be done like this:

fruits = ["apple", "mango", "banana"]

for i in range(len(fruits)):
    fruit = fruits[i]
    print(f"{i}: {fruit}")

# Output:
# 0: apple
# 1: mango
# 2: banana
Enter fullscreen mode Exit fullscreen mode

It gets the job done, but not very pythonic. You have to get the length of the list to keep track of the index, then index into the array to get the current fruit - which makes the code more verbose and harder to read.

A better way: enumerate()

fruits = ["apple", "mango", "banana"]

for i, fruit in enumerate(fruits):
    print(f"{i}: {fruit}")

# Output:
# 0: apple
# 1: mango
# 2: banana
Enter fullscreen mode Exit fullscreen mode

Numbering can also be set to begin at any desired number.

fruits = ["apple", "mango", "banana"]

for i, fruit in enumerate(fruits, 7):
    print(f"{i}: {fruit}")

# Output
# 7: apple
# 8: mango
# 9: banana
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bgatwitt profile image
bga

Cool. Thanks.