Enumerate Functions In Python. Topic – 28

Enumerate function in python

The enumerate function is a built-in Python function that gives us the index number of the element with an iterable (like list, tuple, or string). With this, we get the element and its corresponding index during the iteration.

Sometimes when we use the for loop we require the index number or position of the elements. Sometimes we want to perform a specific task depending on the position of the elements. Normally we can do this task using a temporary variable, and increase the value of the variable over each iteration. Here’s a basic example of how it works without enumerating functions:

my_lists = [12,45,17,5,41,58,98,45,15,78,15]

index = 0 #temporary variable
for my_list in my_lists:
    if index == 4:
        print("Index 4 is running")
        #increment the value of index over each iteration
    index = index + 1

The output of this code will be:

Index 4 is running

This code will be working fine but I think, there is a huge headache to perform this simple task. So, there is another simple function to perform this task more easily. This function is called enumerate. Here’s a basic example of how it will look like:

my_lists = [12,45,17,5,41,58,98,45,15,78,15]

for index, my_list in enumerate(my_lists):
    if index == 4:
        print("This is postion 4")

The output of this code will be:

This is postion 4

The enumerate function will give us the index number automatically with the expected value of the loop. In the same way, we can also use strings instead of numbers.

The most important thing to remember is that in the syntax “for index, my_list in enumerate(my_lists)” the variable that will be placed first will store the index value in it. In this example, the index value will be stored as the index variable. It is not necessary to keep the index number in the index variable. In place of the index, we can put chocolate, game, video, or anything.

The interesting fact is by default index number starts from 0 If we want to start the index value from 1, we can do that as well. By default first element’s index number is 0, the second element’s index number is 1, and so on. But we can set the first element to 1 and the second element to 2 and so on. For achieve this just do a simple steps:

fruits = ['apple', 'banana', 'mango', 'kiwi', 'Orange']

for index, fruit in enumerate(fruits, start=1): #start indexing from 1
    print(index, fruit)

The output of this code will be:

1 apple
2 banana
3 mango
4 kiwi
5 Orange

For example, you might use it to loop over a list of strings and print the index and value of each string in a formatted way:

fruits = ['apple', 'banana', 'mango', 'kiwi', 'Orange', 'Pineapple', 'Watermelon', 'Lemon']

for index, fruit in enumerate(fruits):
    print(f'{index+1}: {fruit}')

The output of this code will be:

1: apple
2: banana    
3: mango     
4: kiwi      
5: Orange    
6: Pineapple 
7: Watermelon
8: Lemon

If you don’t know the Finally Keywords In Python” just click on Read More.

I hope you understand “Enumerate Functions In Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

Your email address will not be published. Required fields are marked *

Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog