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.
Summary
This is how Python enumerate works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
