Introduction
Welcome to my 53rd blog post on the Python journey. On day 53, I learned about higher-order functions map, filter and reduce. I learned about how they take functions as an argument and apply them to a sequence of iterable objects. Let's dive into more details and understand these higher-order functions in Python.
So let's get started......
MAP, FILTER & REDUCE
These functions are higher-order functions, as they take functions as an argument. They are built-in functions that allow you to apply a function to a sequence of elements and return a new sequence.
Map
It applies a function to each element in a sequence and returns a new sequence containing the transformed elements.
Syntax -
map(function, iterable)
The function argument is a function that is applied to each element in the iterable argument. The iterable argument can be a list, tuple, or any other iterable object.
Example -
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Double each number using the map function
doubled = map(lambda x: x * 2, numbers) #this returns map object hence we convert to list below
# Print the doubled numbers
print(list(doubled))
In the above example, the map function applies the lambda function to each element in the list and returns a new list containing the doubled numbers.
Filter
It filters a sequence of elements based on a given predicate (a function that returns a boolean value) and returns a new sequence containing only the elements that meet the predicate.
Syntax -
filter(predicate, iterable)
The predicate argument is a function that returns a boolean value and is applied to each element in the iterable argument.
Example -
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Get only the even numbers using the filter function
evens = filter(lambda x: x % 2 == 0, numbers)
# Print the even numbers
print(list(evens))
In the above example, the lambda function lambda x: x % 2 == 0 is used to filter the numbers list and return only the even numbers.
Reduce
This higher-order function applies a function to a sequence and returns a single value.
It is a part of the functools module in Python and is imported whenever it is used. We cannot directly use the reduce function
The function argument is a function that takes in two arguments and returns a single value.
The reduce function applies the function to the first two elements in the iterable and then applies the function to the result and the next element, and so on. Then the reduce function returns the final result.
Syntax -
reduce(function, iterable)
Example -
from functools import reduce
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Calculate the sum of the numbers using the reduce function
sum = reduce(lambda x, y: x + y, numbers)
# Print the sum
print(sum)
In the above example, the reduce function applies the lambda function to the first two elements in the list (1 and 2) i.e it sums 1 and 2 and then applies the function to the result (3) and the next element in list (3), and so on. The final result is the sum of all the elements in the list, which is 15.
NOTE - reduce function requires the functools module to be imported to use it.
Resources Used
You can watch the video of Day#53 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 53, I learned about using map, filter and reduce to take arguments as functions in Python. They apply functions to a sequence of elements and return a new sequence.
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 #53
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.