THE DEFINITIVE GUIDE
Everything you need to know about enumerate()
Use python’s enumerate function to change the way you loop forever
The first exposure to loops in python usually starts with looping over a basic sequence. One of the functions you learn about to do this is the range function. The range function returns a sequence of numbers and used to loop over an item in a sequence.
Sometimes it’s necessary to need to know the index of a sequence, that is the order of the sequence. Whenever you see within your code range(len(…)) there is a better way to loop than through using an index! This comes up in programming more than you would think! This is where the built-in function enumerates can help.
In this article you will be exposed to a built-in function that I will convince you is more readable and will change the way you do looping in the future.
In this article, you will learn
- The basic syntax of the enumerate built-in function
- Understand what the enumerate object does
- Understand behind the scenes of the enumerate function
- Examples of the enumerate function in simple code
- How to unpacking sequences