Ultimate beginners guide to using the enumerate function in Python

How to Use Python's Enumerate Function

Dylan | Jul 13, 2020

Post Thumbnail

In my experience, Python’s enumerate function is often skipped in beginner learning materials. Perhaps this is actually a good thing for absolute beginners by forcing them to use basic for loops and build a foundational understanding of them. However, as we progress in our Python journey, this function helps us produce cleaner code.

When can you use it?

What if we wanted to loop through a list of ice cream flavors and print them for others to view, but this weekend is the 4th of July and in celebration, you would like to offer a discount for the 4th flavor on the list. Let’s look at two ways we could achieve this using generic for loops.



flavors = ['chocolate', 'vanilla', 'cookies and cream', 'superman', ‘strawberry']

# Option One
counter = 0
for flavor in flavors:
counter += 1
if counter == 4:
print(flavor + ' 50% Off!')
else:
print(flavor)

# Option Two
for i in range(len(flavors):
if i == 3:
print(flavors[i] + ' 50% Off!')
else:
print(flavors[i]


When I began coding, I wrote many loops following the pattern in Option Two but I always felt like there must be a better way. It turns out there is! Let’s rewrite the example above using the enumerate function.



flavors = ['chocolate', 'vanilla', 'cookies and cream', 'superman', ‘strawberry']

for i, flavor in enumerate(flavors):
if i == 3:
print(flavor + ' 50% Off!')
else:
print(flavor)




Intuition and Syntax

Continuing with the previous ice cream example, the enumerate function stores the current index of the loop in the first variable name before the comma ('i' in our case). The variable following the comma ('flavor') stores the item at the current index, just like in a normal for loop.

Simultaneously, we can log or edit indices and still have direct access to the current object in that iteration of the list. What’s interesting is how Python handles this current index and current object pair. Take a look at the following code.



flavors = ['chocolate', 'vanilla', 'cookies and cream', 'superman', ‘strawberry']

enumerate_flavors = enumerate(flavors)
print(list(enumerate_flavors))
Output: [(0, 'chocolate'), (1, 'vanilla'), (2, 'cookies and cream'), (3, 'superman), (4, 'strawberry')]


Under the hood, it simply builds a list of tuples and by iterating a list with two values inside the tuple (i and flavors), we can access both the index and object stored at that index!

Optional Parameter (start)

The enumerate has an optional parameter, called start. By setting this value to an integer, we can set the first index value of the enumerate function. If we set start equal to 100, the first value in the first tuple will not be 0. It will be 100 and the first value in the second tuple will be 101.



flavors = ['chocolate', 'vanilla', 'cookies and cream', 'superman', ‘strawberry']

enumerate_flavors = enumerate(flavors, start=100)
print(list(enumerate_flavors))
Output: [(100, 'chocolate'), (101, 'vanilla'), (102, 'cookies and cream'), (103, 'superman), (104, 'strawberry')]


Note, start is the only optional parameter, so it is not necessary to write the parameter keyword 'start'. We can also write enumerate(flavors, 100).

Conclusion

With another powerful tool under your belt, you’re a step closer to becoming a true Python guru. Enumerate isn’t a difficult concept to grasp, so you should be able to start implementing it in your projects now! As always, to cement your understanding of this function, I recommend you launch a python environment and play around with the function yourself. If anything remains unclear, please leave a comment below and I’ll do my best to help! Thanks for reading!

Comments