enumerate() in Python
Overview
The enumerate() is a Python library function used to add a counter to an iterable object and return it as an enumerating object. Enumerating objects can be iterated on, and all the values can be accessed through the loops and indexes.
Syntax of enumerate() in Python
The syntax for the enumerate() method in the python method is pretty simple that is as follows:
Parameters of enumerate() in Python
Below are the parameters of the enumerate function:
- iterable_object: The iterable object whose enumerating object is to be made. It can be a sequence like string and list or any other iterator object.
- start(optional): The start variable from which the counter of the iterable object is being started. If not specified, the value of the start variable by default will be 0.
Return Value of enumerate() in Python
Return Type: <class 'enumerate'>
It returns an enumerating object which can be converted to a list where each value and its counter are stored.
Example of enumerate() in Python
In this example, we are simply passing a list in the enumerate() function and then we will convert the obtained object into a list.
Code:
Output:
Explanation:
After applying the enumerate() function on the list it creates an object which, when printed, gives that the object created is an enumerating object with its address reference, but to see the enumerating values, we have to convert it into a list and then print it.
What is enumerate() in Python?
Suppose there is a case when you have to keep track of indexes with elements in an array or list. Instead of keeping an index variable and increasing it on every iteration, we can apply the enumerate() function to the list, which will do the work.
The enumerate() function in Python is used to add a counter to an iterable object and return it in the form of an enumerating object. Each element and its counter value can now be accessed by converting the enumerating object to a list and iterating over the converted list.
How enumerate() in Python works?
The enumerate() function assigns a particular index to every item in the given iterable object, which can later be used to refer to the item. It helps keep control of the contents of the iterable object easier.
If you use enumerate() in python and then the function, you would get an alphanumeric memory referencing address rather than a readable list. Enumerate python operates in this manner to save space by not constructing superfluous full-fledged lists.
Advantages of using Enumerate:
The following are some advantages of using Enumerate in Python:
- enumerate() function loops through a list, tuple, dictionary, or string and returns the values as well as the index for the particular value.
- You can use list.index to get the index value in a for-loop. List.index(n), on the other hand, is quite expensive because it traverses the for-loop twice. Enumerate is particularly handy in this situation because it provides both the index and the items simultaneously.
- It can also be used at the runtime of the loop also, so we don't have to store the values in extra space.
Disadvantages of using Enumerate:
The following are some disadvantages of using Enumerate in Python:
- Enumerate() function uses more space complexity than the normal list.
- Enumerating object needs to be converted into a list or tuple, thereby taking extra space and time complexity in list conversion.
More Examples
Let’s take a look at some more examples of the enumerate() function in python for understanding.
Example 1: Unpacking Arguments with enumerate() in Python
When you use enumerate python function in a for loop, Python instructs it to utilise two variables: one for the counter and one for the value itself. You may accomplish this by employing a Python technique known as argument unpacking.
Argument unpacking is the concept that depending on the length of the sequence, a tuple might be divided into many variables.
Let us take an example where we are using zip() and enumerate() by using nested argument unpacking:
Code:
Output:
Explanation:
In this example, we nested zip() within enumerate in the for loop (). This implies that enumerate() returns a tuple with the very first value as that of the count as well as the second value as an other tuple comprising the entries from the zip parameters every time with a for loop iterates. To unpack the layered structure, we used parentheses to grab the items from the zip's nested tuple of elements.
Example 2: Iterating With for Loops
In this example, we are using the enumerate() function at the time of iterating through the loop, and then we can easily get all the values during the loop traversal.
Code:
Output:
Explanation:
When we apply the enumerate function during the execution of the loop, at every iteration, this function will create an object containing the list element and the counter associated with it, so we have printed the object created. In the 2nd loop, we have printed the counter value and the list element in every iteration.
Example 3: Using enumerate() on a list with startIndex
In this example, we will use the optional parameter 'start' for setting the start value of the counter index.
Code:
Output:
Explanation:
In the first Loop, we have set the starting index as 10, so the counter values will be starting from 10 and so on. In Second Loop, we have set the starting index as 5, so the counter values will be starting from 5 and so on.
Example 4: Enumerating a String
In this example, we will be using a string value for creating an enumerating object, as a string is also an iterable object.
Code:
Output:
Explanation:
After applying the enumerate() function on the string value, it creates an object from which values are used.
Example 5: Enumerating a Tuple
In this example, we will use a tuple to create an enumerating object, as a tuple is also an iterable object.
Code:
Output:
Example 6: Enumerating a Dictionary
As a Dictionary is also an iterable object, we will be using a Dictionary for creating an enumerating object. But the keys of the dictionary will be used for creating the enumerating object.
Code:
Output:
Explanation:
Here the keys of the dictionary are beginning used for creating the enumerating object, which can be seen in the output.
Conclusion
-
The enumerate() Function is a Python library function that is used to add a counter to an iterable object and returns it as an enumerating object.
-
Every element and its counter value can be accessed by converting the object to a list and by using a loop by iterating over the converted loop.
-
enumerate() comes particularly handy in this situation because it provides both the index and the items all at once.
-
It can also be used at the runtime of the loop, so we don't have to store the values in extra space.
-
Useful Tips for Using Enumerate in Python
- You cannot just index the enumerated object to obtain specific tuples. To access individual items in the object, use the list() or tuple() methods to turn the enumerated object into a list or tuple.
- Now, the converted list or tuple can be accessed easily using indexes.
- Using the traditional for loops is messy, time-consuming, and space-consuming. So, you can always use enumerate in python to make the code look cleaner and save on memory and time.