In Python, enumerate() is a built-in function that makes it easy to iterate over a sequence such as a list or tuple, and keep both the index and value of each item in mind while doing so. It yields an iterator that produces pairs with the index and the corresponding item from the sequence, making it easier to access both simultaneously. This helps when one needs to know the position of elements while iterating through them; this improves code readability and reduces the need for manual index tracking.
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.
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.
Dive into Machine Learning
Achieve More with Machine Learning Training
|
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.
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.
AI and Data Science Mastery Program
Shape Your Future in AI and Data Science
|
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.
Our Python Courses Duration and Fees
Cohort starts on 11th Jan 2025
₹20,007
Cohort starts on 11th Jan 2025
₹20,007
Cohort starts on 11th Jan 2025
₹20,007