Most of the newcomers and even some advanced programmers are unaware of the built-in function enumerate. Itās used to loop over an array or string and have an automatic counter.
Hello everyone and welcome to the article where we are going to learn everything that you need to know about python enumerate.
What we will cover in this article:-
- What is Python enumerate?
- Can you enumerate a string python?
- What does enumerate mean in programming?
- Why enumeration is used in Python?
- What is the difference between enumerate and list?
- What does the function enumerate () do when applied on a Python dictionary?
What is Python enumerate?
One of the many built-in functions of python is enumerate ( ), it can be used when iterating while keeping count of the iterationās index.
This enumerate() function takes a collection as an argument, this could be an array or a tuple. It returns an enumerate object ( ).
This object can be used in for loops. Bypassing this object to a list method it could be converted to a list of tuples that are index-value pairs. The enumerate() function also adds a counter that is the key to every enumerate object.
Syntax
enumerate(iterable, start)
Parameter | Description |
iterable | A sequence, an iterator, or objects that supports iteration |
start | Starts counting from this number. Default is 0 |
What does enumerate mean in programming?
In this example, we create a list called my_listĀ that contains 4 strings. Then a for loop iterates over the enumerate object created from this list.
In every iteration, a counter is the index of the value that corresponds to every element of the array. When running this script, the following result is displayed:
Now if we add the second argument, enumerate( my_list ) will become enumerate( my_list, 3 ), and the index will start from 3 instead of 0:
The following result is displayed:
We can use the optional argument of enumerate when creating tuples containing the index and list item using a list. Here is an example:
The following result is displayed:
Example using tuple
As mentioned above, you can enumerate over a tuple as well since tuples are iterable objects. This method is similar to when we enumerate over a list.
The following result is displayed:
Why enumeration is used in Python?
Instead of using the for loop, an enumerate function could be used with the same result because it has by default a counter that can be used as an index. We can also loop over the value of the array/list at the same time.
Cleaner code is achieved by using enumerate (), now we write fewer lines of code while getting the same result. For example:
Using a for loop to do the same thing as the code above would look something like this:
Both of them displays the same result:
This code isnāt just longer. This way of using a for loop is a bit hard and not straight forward and anyone could easily make a mistake when using it.
Can you enumerate a string with Python?
In Python, strings objects are by default iterable. That means you can use enumerate () the same way you would with any other iterable object:
The following result is displayed:
Every character from the string is considered as a value and given an index, then when we call the print function, each character is displayed by itself.
Example using strings
First, we declare a string, named string, of value āHELLOā. Using the enumerate object with an argument string, in every iteration, the index and character are stored in variables and then displayed using the print function.
And the result is the following:
Enumerate a list of tuples
In this example, we will create a list of tuples that contains name-age pair.
The conventional way to enumerate the list is by using a for loop. We start by creating an array that is a list of tuples, called people. Each element of the array is a tuple that contains the name and age of a person, the name is a string and the age an integer.
We know itās a list of tuples and not lists because every element is declared between parentheses and not brackets. Using a for loop, we iterate over the enumerate object from the people list.
In every iteration, id and val are assigned the index and value of each element respectively. The val variable is a tuple, and to access elements from this tuple we need to use brackets, and between them which element we need.
In our case val [0] is the name and val [1] is the age. And those variables change with every iteration. Now we can simply display the result using a print function. Id is the counter that the enumerate function creates, this will be the index.
And we will get the following result:
However, a cleaner way to achieve this is through tuple unpacking
With tuple unpacking, we can do something like this
The same result is achieved but in a simpler way. Instead of storing the tuple in a variable in every iteration and then reaching its elements with index 0 and 1.
When can directly store the values in two separate variables using the tuple unpacking. It works because both of them are between parentheses. Now we can simply use the variables name and age in the print function.
What does the function enumerate ( ) do when applied to a Python dictionary?
Dictionaries and sets are not sequences, they donāt have and donāt need an index. And to use the enumerate function we should care about the index, which is not the case.
Itās not useful to enumerate with a dictionary argument. The same is applied for sets Instead we could iterate over the keys or values of a dictionary using the following code.
The result is the following:
To iterate over a set:
The following result is displayed:
What is the difference between enumerate and list?
The list() built-in function returns a list. If no parameters are passed, it returns an empty list. If iterable is passed as a parameter, it creates a list consisting of iterableās items.
The following result is displayed:
The list function creates an array containing each character from the passed string as an element. The same result is obtained when passing a tuple as an argument. This argument is then converted to a python list.
Now itās time to explore our Python Enumerate video tutorial.
Tasks to practice
To make sure that everything we explained was clear and understood, try to complete the following tasks and test your python skills using the enumerate function.
- Create a list of grocery list and display it with an index for each item.
- Using the same list, add a brand for each item using a for loop
- For all the items that have indexes greater than 5, remove them from the list by changing the stringās value to ācheckedā
This article comes to an end here, I hope everything was clear. Now itās time to explore our video tutorial.
If you have any questions donāt hesitate to drop a comment in the section below.