• Articles
  • Tutorials
  • Interview Questions

Enumerate() Function in Python - A Detailed Explanation

Tutorial Playlist

What Is Enumerate in Python?

Before we jump into understanding What is enumerate in Python, let us unfold the chapter by finding out what enumerate Python is and what it means to enumerate something. The term ‘enumerate’ is commonly used in mathematics and computer science, which is nothing but ordered listing of all elements in a collection.
Enrol yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!

Now that we have an idea about what is enumerate in Python, in general, means, we can go ahead and start working on the python enumerate function. Following is the list of all topics covered in this module.

So, without further ado, let’s get started to learn more about Python Enumerate.

Python Enumerate Function

As mentioned earlier, in order to perform enumerate in Python, the Python programming language offers a simple function that not only makes iterating operations a lot easier but also helps our program look cleaner.
Let us prove this with the help of an example.

List = [“Red”, “Green”, “Black”, “Blue”]
for i in range(len(List)):
print(i, List[i])

Output:

0 Red
1 Green
2 Black
3 Blue

But, the same thing can be achieved by one simple function without using range and length functions, which can become tricky at times.
Let us see how Python enumerate makes it less complicated.

Become a Python Expert

Enumerating a List

With the help of Python enumerate function, we can iterate over the index and value in a Python list by using a basic for loop. Let us see how:

List = [“Red”, “Green”, “Black”, “Blue”]
for i, j in enumerate(List):
print(i, j)


Output:
0 Red
1 Green
2 Black
3 Blue

Even though both programs have same length, just by looking at both, we can tell that the second one with the enumerate function in Python is less complicated. One function is doing all the magic.

Fun fact

With the help of enumerate, we can create a Python Dictionary out of a list very easily. Let us see how:

List = [“Red”, “Green”, “Black”, “Blue”]
Dict= dict(enumerate(List))
print(Dict)

Output:

{0: ‘Red’, 1: ‘Green’, 2: ‘Black’, 3: ‘Blue’}

Interesting, right? Indeed! Well, not only in list, enumerate can be applied to other Python data types as well. Now, let us go ahead and see how to use enumerate Python in different data types in Python.

Go through this Python Course in London to get a clear understanding of Python!

Certification in Full Stack Web Development

Enumerating a Tuple

Just the way we performed enumerate Python in a list, we can enumerate a Python tuple. Let us try this out with a simple example.

Example:

List = [“Red”, “Green”, “Black”, “Blue”]
for i, j in enumerate(List):
print(i, j)

Output:

0 Red
1 Green
2 Black
3 Blue

Now that we have learned how to enumerate a list and a tuple, let us see how to enumerate a list of tuples.

Enumerating a List of Tuples

Example:

List1 = [(10,”Red”), (5,”Green”), (8,”Black”),(2,”Blue”)]
for idx, (count, color) in enumerate(List1):
print(idx, count, color)

Output:

0 10 Red
1 5 Green
2 8 Black
3 2 Blue

This method of getting values out of a tuple is also referred to as tuple unpacking.
Alright, let us move ahead and see how to apply enumerate over a Python String.

Get 100% Hike!

Master Most in Demand Skills Now !

Enumerating a String

Example:

String = “Python”
for i, j in enumerate(String):
print(i, j)

Output:


0 P
1 y
2 t
3 h
4 o
5 n

Well, we have to note that enumerate in python, by default, starts from index 0, which is considered as the starting index. Now, with the help of the enumerate function in Python, we can also specify the index that we want to operate the enumerate function in Python from.

Learn more about Python from this Python Training in New York to get ahead in your career!

Enumerating from a Specific Index

Let us view how to enumerate from a specific index and not from index 0 with the help of an example.

Example:

String = “Python”
for i, j in enumerate(String, 1):
print(i, j)

Output:


1 P
2 y
3 t
4 h
5 o
6 n

This often comes in handy when we don’t want to start the iteration from 0.

Become a Full Stack Web Developer

Enumerating Inside a List Comprehension

Enumerating inside a list comprehension makes things very less complicated for programmers.

Example:

String = “Python”
for i, j in enumerate(String, 1):
print(i, j)

Output:

1 P
2 y
3 t
4 h
5 o
6 n

With this, we come to the end of this module in Python Tutorial. Now, if you want to know why Python is the most preferred language for data science, you can go through this blog on Python Data Science tutorial.

Further, check out our offers for Python Online Course Course and also refer to the trending Python coding interview questions prepared by the industry experts.

Course Schedule

Name Date Details
Python Course 05 Oct 2024(Sat-Sun) Weekend Batch
View Details
Python Course 12 Oct 2024(Sat-Sun) Weekend Batch
View Details
Python Course 19 Oct 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Senior Consultant Analytics & Data Science

Presenting Sahil Mattoo, a Senior Consultant Analytics & Data Science at Eli Lilly and Company is an accomplished professional with 14 years of experience across data science, analytics, and technical leadership domains, demonstrates a remarkable ability to drive business insights. Sahil holds a Post Graduate Program in Business Analytics and Business Intelligence from Great Lakes Institute of Management.