Python

Enumerate python examples

Enumerate () is present in the python library and is a built-in function. As an input, it takes a collection of words and gives them an enumerated object. Enumerate function is similar to the for loop in other languages like C. Enumerate () function always acts as an iterable object that counts in each iteration. The start index or start parameter is optional in the enumerate function code. The enumerated object obtained as output is then directly used for further amendments like usage in for loops etc.  Enumerate () function helps us to allow looping over strings, dictionaries, and lists and display the output with data indexes. Taking advantage of enumerating as compared to using loops is a pythonic way in the programming sector. It also makes the code clean and easily understandable. Because as we extend the code, it becomes messier and causes many errors.  In this article, we will cover different examples to see the functionality of Enumerate function.

Syntax of enumerate()

Enumerate (Iterable object, starting index)

There are two parameters of enumerate function.

Iterable: An object that is used to support iteration like loops.

Start index: It is an optional parameter that is used to start iteration from the specific index. If starting index is not mentioned, it is considered as zero then,

Apply enumerate() on the list

Consider a list named Sample having the names in it. We will take another temporary list and apply the enumerate function on the sample list to store the output in the sample1 list. List() function is used to display the resultant output of enumerating python function.

To see the results, we will either run the program on the spyder tool or have the involvement of Linux; we need to open the terminal to execute the command.

$ python3 /home/aqsa/sample.py

Here we have used the word python3, as the source code is in python, whereas 3 shows the version number. Following the python, the name of the file I have written or the whole path address of the respective file. As the extension shows, .py is the extension of python.

The output shows that the index number is allocated to each word present in the list.

With start index, apply to enumerate() on the list

We have taken the same above example to elaborate the usage of the start index in the list. Here the difference between the above one and this example is that we have provided the index number from where we want to start giving the number to the data present in the list. By default, the index number is started from 0; as we have already seen in the above example, we don’t have provided the index number, so it is started from the 0 index value.

>> sample1 = enumerate(sample, 8)

In this example, instead of using the Linux terminal, we have run the query on the tool we are using as a text editor(i.e., spyder). The respective output shows the index number starting from the number we have mentioned in the code.

Apply Loop function on the Enumerate ()

For loop has three parameters start, end, and increment. But here in python, we have just used the variable to start and provided the function with the index number from where we want to start displaying the output. The syntax for the FOR loop is appended below:

>> for I in enumerate(sample,22)

The print function will pass the variable as a parameter to display the content.

We will display the output in the Linux Ubuntu terminal. The output shows the index number starting from 22 and onwards with each word present.

Apply enumerate() function on a string

Every item is a character in a string. By applying this function, we will get the character value and character index. Enumerate (string name) is a simple function syntax used to apply his function on the string.

In this illustration, we have taken a string “python3”, whose name is a string.  Here we will utilize the FOR loop to excess the whole string. The loop functionality in the enumerate function is already discussed above in the article.

As it is clear from the output, the index number is allotted to each character of a string.

Apply enumerate() function on the python dictionary

If you want to apply an enumerate function to the dictionary, you will use the function item () with the dictionary’s name. Here we have taken a dictionary having both keys and values. The chosen procedure for this example is quite different from the others as we have taken two variables to indicate the index. One is for the key, and another one is for the value. The output will be obtained by using the for a loop.

$ for k,j in dictionary.items():

The output of the program is shown below by using the Run process in the Spyder tool.

Apply Conditional Statements on enumerate () function

You must be aware of the conditional statements in different languages. One of the most usually used conditional statements is the if-statement, which we have used in our example. The conditional statement is used to skip other unwanted items in a single iteration of a loop. Here in this illustration, we have taken sample data. We want to print only one word that is present on index 1. As in this function, we don’t have to mention any starting index, so by default, it is zero.

>> if index = =1:

So the condition will check the index; if it is equal to the required index, then the output is displayed. As shown below, “class 3” is present on index 1.

Conclusion

In this article, we have seen the alternative of complicated loops, and we have discussed the basic difference between loops and the enumerate function. Further, the enumerate () method application on strings, dictionaries, and the list is explained in an elaborated manner.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.