At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

The Uses of Enumerate() Function in Python

  • By Rajasekar
  • February 19, 2020
  • 2848 Views

Enumerate() in Python is a built-in function and it was added to Python along with the implementation of PEP 279.
Most time, we need to keep track of the number of iterations in many cases. So, Python developers got to say thanks to the enumerate() function. This lets you count the number of times it is iterated. This inbuilt function adds a counter to an iterable and returns it in the form of an enumerate object. This object can then be used in for loops or can be converted into a list of tuples using list() method.
An enumerator() function in Python adds a counter of iterable numbers to the provided data structure. With respect to the data type such as integers, characters or strings, the data structure might be any list, tuple, dictionary or sets.

In this blog, you will come to know When, Why and How you can use enumerate function in Python. And where in Pythonic loops to write highly readable codes. Before that, we will get to know the best way for looping logic if you want the index of iterated values. let’s look at it.
The classic method of a programmer from other languages or some will do! Here is the list of some classic methods.

The Classic Methods

1#

fruits = ['Apple', 'Orange', 'Mango']
listOfFruits = []
n = 0
for fruit in fruits:
    listOfFruits.append((n, fruit))
    n += 1
print(listOfFruits)

Output

[(0, 'Apple'), (1, 'Orange'), (2, 'Mango')]

In Python, this is what beginners do!
They use two built-in function range() and len() to achieve their goal.
2#

fruits = ['Apple', 'Orange', 'Mango']
for i in range(len(fruits)):
    print(i, fruits[i])

Output

0 Apple
1 Orange
2 Mango

The Uses of Enumerate() Function in Python

When it is used
If they want the index of iterate values.
How it used
The enumerate function assigns an index to the list of each iterable value.
Why it used
When we are working with iterators, there is always a need for getting the count of iterations. The above snippets are examples of a few programmers.
But we have a pythonic way to find the count of iteration for needful. Here is the syntax.

Enumerate () Function Syntax

enumerate(iterable, start=0)

Enumerate is a function and you have to pass two arguments in which one is optional, i.e start=0 by default.

  • Iterable – Objects that support iteration
  • Start (optional) – It starts counting from this number.
Arg 1: iterable #values(list, tuple, string)
Arg 2: start #value (integer input, like 100)

Examples

Using a List

List of fruits with no track of fruit. For our need to get the fruit index or count of fruit, we can try to get it with a simple example 3#.
3#

fruits = ['Apple', 'Orange', 'Mango']
for the count, fruit in enumerate(fruits):
    print(count, fruit)

Output

0 Apple
1 Orange
2 Mango

Using a Tuple

It is similar to what we did with the List, but here we use a tuple of fruits with no track of fruit. We are doing this to our need to get the fruit index or count of fruit with a simple example 4#.
4#

fruits = ('Apple', 'Orange', 'Mango')
for the count, fruit in enumerate(fruits):
    print(count, fruit)

Output

0 Apple
1 Orange
2 Mango

Using a String

String data iterate using enumerate function and get the index of each character.
5#

fruit = 'Apple'
for the count, f in enumerate(fruit):
    print(f"{count} index character of the fruit is {f}")

Output

0 index character of the fruit is A
1 index character of the fruit is p
2 index character of the fruit is p
3 index character of the fruit is l
4 index character of the fruit is e

List of Tuple and Vice-Versa

List of Tuple or Tuple of List. Here we are using fruits along with colour. Actually, packing and unpacking the variable or data is pretty simple in python.
6#

fruits = [('Apple',("red","green")), ("Orange",("orange")), ("Mango",("yellow"))]
for count, (fruit,color) in enumerate(fruits):
    print(count, fruit, colour)

Output

0 Apple ('red', 'green')
1 Orange orange
2 Mango yellow

Another Parameter Usage in Enumerate

7#

fruits = ('Apple', 'Orange', 'Mango')
for count, fruit in enumerate(fruits,start = 100):
    print(count, fruit)

Output

100 Apple
101 Orange
102 Mango
  • Enumerate is a built-in function in python with an enumerate object.
  • This function returns an object of type enumerate.
  • To pass the iterable object to enumerate function, try to get data from the enumerate object firstly and then convert it to list(). So, it returns a list of tuple along with the iterate index as shown in the example below.
  • For Enumerate object implementation details, refer to this link

Example

print(type(enumerate(["A","B"])))
print(list(enumerate(["A","B"])))

Output

<class 'enumerate'>
[(0, 'A'), (1, 'B')]

Alright, that is everything you should know about Python Enumerate () function. You might be liking the other Python blogs such as Top Python use cases and Real-world applications.
Get in touch with Agira technologies, one of the best web development companies present in the industry to build the best web application for your business.

Looking for a Tech partner to dominate the digital world?

Rajasekar

A Senior Software developer. Specialist in Python, Django, Flask, PostgreSQL, and AWS. Currently working on Machine Learning, Raspberry Pi and NLP technologies. This ecstatic techie is always full of ideas that make life easier with machines. He is also a biker by passion.