What is the Python enumerate function?
Python enumerate is a built-in python function that basically lets us add a counter when we are working with an iterable object.
In this article, we are going to use a list as an example and we are going to explore when and where to use the enumerate function. Taking this list as an example, we might want to loop through each item in this list using a for a loop. Besides this, we might also want to know the index or count of the list in the loop as we go through.
How can we use the Python enumerate function?
Now one way to do this would be to use range. We can do this by writing for x in range and then use “len” for the length to determine the length of the list. After the colon, we can now write a print statement in the next line that prints both x and the index of x. As shown in the code below.
This is not the best way to do this since the output looks a little bit untidy. We are going to use the enumerate function instead. If we print enumerate on my list the output will return an enumerate object.
Using Python enumerate function in a for loop
Enumerate can also be used together with a for loop. We can do so by enumerating every item in the list and then printing out the item. The enumerate function is going to add a counter to our list and the output is going to be a tuple with index numbers with their corresponding items.
Now another thing that we can do is we can actually have a start number when asked to enumerate our list. So that instead of starting at the default index of zero as in the example above, we can actually have a start number. equal to and in the example above let’s add one as the start number.
In this example, the counter starts at one. Although we can do this using the range and the for loop. The enumerate function is built for this.
However, since this returns a tuple, we can actually just amend our follow up a little bit and we can print out both of the numbers and the items from this. Instead of an item, we can use x,y and then print out x,y again where x is going to represent the count from the enumerate that we get which is the first item in the tuple and then y is going to be the value, which is the second item.
Note that the index numbers start from 1 since we still have “start=1”. And if we were to remove that then the count will begin from zero as shown below.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.