Introduction
Welcome to my 42nd blog post on the Python journey. On day 42, I learned about the enumerate function in Python that can be used to loop over a sequence. I also wrote a bunch of code to practice the concept of enumerate function. Let's dive into more details.
So let's get started......
Enumerate Function
It is a built-in function in Python that allows to loop over a sequence (such as a list, tuple, or string)
It helps to get the index and value of each element in the sequence at the same time. Here's a basic example of how it works:
Example 1 -
In the below example, we make use of the enumerate function to loop through the sequence. The same can be done using normal for loop but it can be cumbersome and makes the code lengthy.
marks = [12, 56, 32, 98, 12, 45, 1, 4]
# index = 0
# for mark in marks:
# print(mark)
# if(index == 3):
# print("Harry, awesome!")
# index +=1
for index, mark in enumerate(marks):
print(mark)
if(index == 3):
print("Harry, awesome!")
12
56
32
98
Harry, awesome!
12
45
1
4
Example 2 -
# Loop over a list and print the index and value of each element
fruits = ['apple', 'banana', 'mango']
for index, fruit in enumerate(fruits):
print(index, fruit)
Output -
0 apple
1 banana
2 mango
Note - As it can be seen, the enumerate function returns a tuple containing the index and value of each element in the sequence. For loops can be used to unpack these tuples and assign them to variables, as shown in the example above.
Changing the start index
The start index for the enumerate function is '0' by default. This can be changed by specifying a different starting index by passing it as an argument to the enumerate function.
Example 1 -
# Loop over a list and print the index (starting at 1) and value of each element
fruits = ['apple', 'banana', 'mango']
for index, fruit in enumerate(fruits, start=1):
print(index, fruit)
1 apple
2 banana
3 mango
Example 2 -
fruits = ['apple', 'banana', 'mango']
for index, fruit in enumerate(fruits):
print(f'{index+1}: {fruit}')
1: apple
2: banana
3: mango
In addition to lists, you can use the enumerate function with any other sequence type in Python, such as tuples and strings. Here's an example with a tuple:
Example -
# Loop over a tuple and print the index and value of each element
colors = ('red', 'green', 'blue')
for index, color in enumerate(colors):
print(index, color)
And here's an example with a string:
Example -
# Loop over a string and print the index and value of each character
s = 'hello'
for index, c in enumerate(s):
print(index, c)
Resources Used
You can watch the video of Day#42 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. Through this Blog post, we learned about a Shorthand way to write code in a sequence using enumerate function. Enumerate function can be used to loop through the list, tuple and strings. We also wrote a bunch of code to practice the Enumerate function.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #4
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.