Python Map, Filter, and Reduce | by Kantajit Shaw | Towards Data Science

In Python, a function can be defined using def. Another way to write small functionality is to use lambda. Lambda functions are inline anonymous functions. It contains only one expression has no explicit return statement. Let’s see some examples.
from time import time_nsdef cubed(x):        return x**3lambda_cubed = lambda x:x**3start = time_ns()print(f"Cube of 9 is {cubed(9)}")print(f"Time taken for previous method : {(time_ns()-start)//1_000} ms")start = time_ns()print(f"Cube of 9 is {lambda_cubed(9)}")print(f"Time taken for previous lambda : {(time_ns()-start)//1_000} ms")start = time_ns()print(f"Cube of 9 is {(lambda x:x**3)(9)}")print(f"Time taken for inline lambda : {(time_ns()-start)//1_000} ms")
In the previous snippet, we will compare the time taken for a function and a lambda function to compute the cube of 9. We can assign a lambda function a name. We can see both of them in the snippet. If we run that, we will get something like this.
Cube of 9 is 729Time taken for previous method : 18 msCube of 9 is 729Time taken for previous lambda : 3 msCube of 9 is 729Time taken for inline lambda : 2 ms
The numbers may vary a little bit every time the snippet is executed. But it is evident that lambda functions are much faster than their method alternatives.
We will now see how we can use a lambda function to optimize our code more. To learn more about Python lambda functions, one can visit this link.
We can use the Python map function to generate a new list from another list. We will compare the loop, and map to generate a list containing squares of 1–10.
start = time_ns()squares=[]for i in range(1,11):        squares.append(i**2)print(squares)print(f"Time taken for a for loop to generate squares: {(time_ns()-start)//1_000} ms")start = time_ns()squares=list(map(lambda i:i**2,list(range(1,11))))print(squares)print(f"Time taken for map and lambda function to generate squares : {(time_ns()-start)//1_000} ms")
The output for the above snippet is...
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]Time taken for a for loop to generate squares: 12 ms[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]Time taken for map and lambda function to generate squares : 9 ms
Similarly, we can use the filter method with a combination of a lambda function to filter out items from the list. In the next example, we will create a list of even numbers from 1 to 10 using the loop and filter method.
start = time_ns()evens=[]for i in range(1,11):        if i%2==0:                evens.append(i)print(evens)print(f"Time taken for a for loop : {(time_ns()-start)//1_000} ms")start = time_ns()squares=filter(lambda i:i%2==0,list(range(1,11)))print(evens)print(f"Time taken for filter : {(time_ns()-start)//1_000} ms")
If we compare the time for each approach, we will see...
[2, 4, 6, 8, 10]Time taken for a for loop : 6 ms[2, 4, 6, 8, 10]Time taken for filter : 4 ms
Reduce
Reduce function works like an aggregation function. The previous methods map and filter are in-built in python, but we need to import functools to use reduce function.
from functools import reduceprint(reduce(lambda x,y:x+y, list(range(1,11))))
The output will be 55. The way reduce works it at first it takes the first two elements of the list and stores in x,y. After performing the lambda function on x,y (in this case sum) reduce function stores the new value in x. Then the next element is assigned to y and the same function is applied to x,y & the result is stored in x, and so on....
It is not necessary to use the lambda function with the map, filter, and reduce functions. Here’s an example of taking a list of numbers as input.
a=list(map(int, input().split()))print(a)
As you can see, they work well with pre-defined Python functions as well.
Once you get familiarized with these functionalities, it is easy to see that these lead to shorter and optimized code. Although sometimes using a for loop can make the code more readable.
But knowledge of these functions isn’t wasted. Next time you are going to preprocess a dataset in Pandas, knowing the concept of lambda function will benefit you.
Click here to explore more about these functions.
Hope you like it. Stay tuned for more.