Introduction
Let’s say that we have a list. We want to iterate over this list, printing out the index followed by the list element or value at that index. Let’s accomplish this using a for loop:
num_list= [42, 56, 39, 59, 99]
for i in range(len(num_list)):
print(i, num_list[i])
# output:
0 42
1 56
2 39
3 59
4 99
range() is a built-in function in python that allows us to iterate through a sequence of numbers. As seen above, we use the for loop in order to loop through a range object (which is a type of iterable), up to the length of our list. In other words, we start at an i value of 0, and go up to (but not including) the length of _numlist, which is 5. We then access the elements of _numlist at the i-th index using square brackets.
However, it is important to understand that we are not actually iterating over _numlist. In other words, i serves as a proxy for the index that we can use to access the elements from _numlist.
Using the enumerate() Function
Instead of using the range() function, we can instead use the built-in enumerate() function in python. enumerate() allows us to iterate through a sequence but it keeps track of both the index and the element.
enumerate(iterable, start=0)
The enumerate() function takes in an iterable as an argument, such as a list, string, tuple, or dictionary. In addition, it can also take in an optional argument, start, which specifies the number we want the count to start at (the default is 0).
Using the enumerate() function, we can rewrite the for loop as follows:
num_list= [42, 56, 39, 59, 99]
for index, element in enumerate(num_list):
print(index, element)
# output:
0 42
1 56
2 39
3 59
4 99
And that’s it! We don’t need to use the range() function. The code looks cleaner and more pythonic.
How enumerate() Works
The enumerate() function returns an enumerate object, which is an iterator. As each element is accessed from this enumerate object, a tuple is returned, containing the index and element at that index: (index, element). Thus, in the above for loop, with each iteration, it is assigning the elements of this returned tuple to the index and element variables. In other words, the returned tuple is being unpacked inside the for-statement:
for index, element in enumerate(num_list):
# similar to:
index, element = (index, element)
These tuples can be more easily seen with the following example:
name_list = ['Jane', 'John', 'Mike']
list(enumerate(name_list))
# [(0, 'Jane'), (1, 'John'), (2, 'Mike')]
Once we call the list() function on the enumerate object (the iterator), it returns a list of tuples, with each tuple including the index and its corresponding element or value.
If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to stories on Medium. If you sign up using my link, I’ll earn a small commission.
Conclusion
In this tutorial, we learned how the enumerate() function is used to count through an iterable.
Share This Article
Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.
Write for TDS