Jun 25, 2022 python

What is Enumeration in Python?

What is Enumeration in Python?

Enumeration in Python :Counting is an element of programming you do every time you code on your computer using different programming languages like Python. Traditionally, you could use the for loop to initiate this, but there is a built-in function you can use to make this process easier. It is the Python enumerating function. So, what exactly is enumeration in Python, and how does it work? To find out, keep reading.

Understanding Enumeration in Python

The Python enumerate function is one of the many built-in functions available in the Python library that you can use to simplify your coding. 

When working with a collection of data in Python, the enumerate() function will save you the time and effort of writing larger programs to get the index and value of a list. 

Enumerate (sequence, start=0) is the fundamental syntax. Again, because enumerate() gives an output containing the index and value, you don’t have to increment the counter yourself as you would do with for loop. It also gives you the option of changing the counter’s beginning value(start=0,1,2,3, and so on).

As arguments, enumerate() takes two variables: an iterable and a starting count value. Both the index and the value are output when the code compiles.

Let’s try creating a custom function like the Python enumerate() function:

def nitro_enumerate(sequence, start=0):

   n = start

    for samp in sequence:

        yield n, samp

        n += 1

How Does Python Enumerate() Work? 

Let’s say you have a list of English words that end with the same sound. Your goal is to loop through the list and retrieve the elements and the index of those elements.

Example 1: Using enumerate() to iterate through a list

Now let’s demonstrate the use of the python enumerate () in solving a similar problem.

nitro_list = [“boot”, “loot”, “tooth”, “foot”, “booth”] 

For x, y in enumerate(nitro_list):

      print(x, y) 


0 boot

1 loot

2 tooth

3 foot

4 booth

Example 2: Using enumerate() to iterate through a string

Nitro_str = “enumerate” 

   for a, b in enumerate(Nitro_str):

       print(a, b) 











Example 3: Using enumerate() to iterate through a tuple

Nitro_tup=[(1,”age range”, 7), (2,”age range”, 11), (3,”age range”, 15),(4,”age range”, 17)]

for a, b in enumerate(Nitro_tup):

    print(a, b)


0 (1, ‘age range’, 7)

1 (2, ‘age range’, 11)

2 (3, ‘age range’, 15)

3 (4, ‘age range’, 17)

Example 4: Using enumerate() to iterate through a dictionaries

Because a dictionary does not have an index and is not always ordered, using the enumeration function might not be necessary. 

You can always iterate

Nitro_tup= {‘India ‘ :1, ‘Dubai ‘ :2, ‘Bangladesh ‘ :3, Isreal ‘ :4,

‘Philippines’ :5, ‘Bangkok ‘ :6,}

for a, b in enumerate(Nitro_tup):

    print(a, b)


0 India

1 Dubai

2 Bangladesh

3 Isreal

4 Philippines

5 Bangkok


You can use this function to enumerate a list, a tuple, a text, or even dictionaries. Again, implementing your own custom enumerate() function using python is great, but the actual code for enumerate() is written in C and this makes compiling faster and more efficient.ppl