Lambda Expressions

Ankit Deshmukh
TechGannet
Published in
3 min readJun 30, 2018

To learn Lambda Expression, first we will look into built in functions, Map and Filter.

Map Function

Map function maps your function to an iterable object. Let’s see an quick example where we are calculating the square of each element of an list:

#numbers.py
numbers = [1,2,3,4,5]
def square(num):
return num**2
print(map(square,numbers))Output:
<map object at 0x7f1060c5df60>

Look at map(square,numbers). First parameter is the function name itself and second parameter is a list. One by one element from list is being mapped to function and square is being calculated.

To print an output, you have to cast map to list type as follows:

#numbers.py
numbers = [1,2,3,4,5]
def square(num):
return num**2
print(list(map(square,numbers)))Output
[1, 4, 9, 16, 25]

Filter function

The filter function returns an iterator for those items for which function is true. You need to filter the function either into True or False, then apply filter function along with function and you will get results for those which would return True.

Let’s see an example where we are checking whether a number is even or not:

#numbers.py
numbers = [1,2,3,4,5]
def check_even(num):
return num % 2 == 0
print(list(filter(check_even,numbers)))Output:
[2, 4]

For elements 2 and 4,function is returning True and that’s why output will be even numbers only.

For elements 1,3 and 5, function is returning False and that’s why output won’t contain these numbers.

Now, let’s move to Lambda Expressions.

Lambda Expressions

Lambda is an inline function which allows us to create anonymous functions.

Lambda’s body is a single expression, not a block of statements. It is less general than def function.

Generally, we define function as:

#numbers.py
def square(num):
result = num**2
return result
result=square(2)
print (result)
Output:
4

Let’s simplify it again:

#numbers.py
def square(num):
return num**2
print(square(2))Output:
4

Can we simplify it again ? Yes.

#numbers.py
def square(num): return num**2
print(square(2))Output:
4

This is an inline function. Try to replicate this function as Lambda does:

lambda num: num**2     #lambda expression to square a number

Let’s assign a name to lambda expression to see the output:

square=lambda num: num**2print(square(2))Output:
4

Let’s see more examples:

  1. Calculate square of elements of a list using lambda. We will go step by step:
lambda num: num ** 2   #lambda expression created

Create a list and map the lambda function to list.

numbers=[1,2,3,4,5]             #list created
map(lambda num: num**2,numbers) #mapping lambda expression to list

To see the output, cast map function to list type.

list(map(lambda num: num**2,numbers)) #casting to list type
#final lambda expression to square the elements of a list

Print the output:

print(list(map(lambda num: num**2,numbers)))Output:
[1, 4, 9, 16, 25]

2. Check if number is even or not:

Create lambda expression first:

lambda num: num%2 == 0     #lambda expression created

Create a list and use filter function to lambda function:

numbers=[1,2,3,4,5]
filter(lambda num: num%2 == 0,numbers)
#mapped lambda expression to list using filter

Cast filter function to list type to see the output:

list(filter(lambda num: num%2 == 0,numbers)) 
#Final lambda expression to find out if number is even or not

Print the output:

print(list(filter(lambda num: num%2 == 0,numbers)))Output:
[2,4]

That’s it! Hope you got the basic understanding of how to use lambda expressions.

Happy Coding!

--

--