Python’s Lambda Magic: Unleashing the Power of Map, Reduce, and Filter

Ashwin B
4 min readJun 12, 2023

Even though Python is a most easy-to-learn programming language, some concepts are still difficult to understand. The lambda function is one of them.

What is a Lambda Function?

These are similar to user-defined functions but without a name a.k.a anonymous function.

Lambda functions are really useful when you want to create functions that contain only simple expressions usually single-line statements, also useful when we want to use the function only once.

Defining a Lambda function:

lambda argument(s) : expression
  • lambda: the keyword used to define an anonymous function.
  • argument(s): accepts values that we want to pass to the function.
  • expression: code that we want to execute using the lambda function.

You’ll see that the anonymous function lacks a return keyword. This is because, after being executed, the anonymous function will automatically return the value of the function’s expression.

Let’s look at an example of how to work with a lambda function:

Assume we want to write a function that returns the square of the number which is passed, the normal way of writing this will be:

def func(x):
return x**2

print(func(2))

#Output
4

Now let’s see how to do the same using the lambda function:

(lambda x : x * 2)(3)

#Output
4

In the above code, we have created a lambda function and accepted an argument called ‘x’, then there is a simple expression that squares the value that is passed to the argument ‘x’.

Because the lambda function is anonymous and does not have a name that may be used to invoke it, we must surround the entire statement when calling it.

Common Use Cases for Lambda Functions:

Using lambda functions with iterables:

In general, anything that consists of a sequence of values, such as characters, numbers, and so on, is considered to be iterable.

When working with iterables, we can use lambda functions in conjunction with three common functions: filter() and map() and reduce()

Filter() function:

Use the filter function to narrow your attention to particular values in an iterable. The syntax of a filter function is as follows:

filter(function, iterable)

It is clear that a filter function needs a different function that contains the expression or operations that will be applied to the iterable.

For example, let us assume we have a list with the values [1, 2, 3, 4, 5, 6].

The task is to filter out and print the values that have a remainder 0 when divided by 2. Let’s see how to achieve this using filter() and lambda function.

we’ll start by creating the equation we want to derive using the lambda function, as seen below:

lambda x:x % 2 == 0

Then let’s insert this into the lambda function as follows:

lst = [1,2,3,4,5,6]
print(list(filter(lambda x:x % 2 == 0, lst)))

#Output
[2, 4, 6]

map() function:

we use the map() function whenever you want to modify every value in an iterable.

map(function, iterable)

For example, let’s say we want to raise all values in the below list to the power of 2. We can easily do that using the lambda and map function like this:

lst = [2,3,4,5]
print(list(map(lambda x:x**2, lst)))

#Output
[4, 9, 16, 25]

reduce() function:

from functools import reduce
reduce(function, iterable)

The reduce function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along

This function can be used by importing it from the “functools” module.

Let’s assume we have a list of values [1, 2, 3, 4] and we want to sum up the values of the list, so we can do this by doing the following

from functools import reduce

lst = [1,2,3,4]
print(reduce(lambda x,y: x+y, lst))

#Output
10
how reduce function works
how reduce function works

If we look at the code we can see that the lambda function accepts 2 arguments x and y. The value of x will be the first value of the list and the value of y will be the second value of the list, in our case x=1 and y=2. Now the expression x+y will be adding the values and give a new value which is 3 in our case. This 3 will now be the new value of x and the value of y will be the next value of the list which is 3. Now these values will be added and a new value be obtained which will be now the value of x. This process will be repeated until the end of the list. Looking at the image will, even more, give us better clarity.

Conclusion:

In this tutorial, we learned the basics of the lambda function and how we can commonly apply it. Thank you for taking the time to read this.

--

--

Ashwin B

I love the internet, technology and building beautiful things