Hi everyone in this tutorial, we will learn about different ways to utilize the lambdas in python efficiently using several examples. Let’s start the tutorial with the definition of the Lambda function.
1. What is Python Lambda function?
Lambda function is also known as an anonymous function which is not bound to an identifier that means they can be defined without a name. These functions are often passed as arguments to the higher-order functions, or used for constructing the result of a higher-order function that needs to return a function. These functions are defined by using a lambda
keyword and a typical syntax for a lambda function looks like the following. This syntax can take any number of arguments but there will be only one expression.
2. Different Ways to use Lambda functions
2.1 Lambda function as a normal function
Let’s create a lambda function which returns the square of the input integer. If we use the normal function it would look like below.
def square(x):
return x*x
print(square(10))
Output:
100
Let’s create a lambda function to achieve the same result.
square = lambda x: x*x
print(square(10))
Output:
100
In the above code, we have simply created a lambda function lambda x: x*x
. Then we have created the function object named square and call that with value 10
in the next line. Now because we have told above that it does not require a name, Let’s see that part in the action without assigning a name to the function to use it. See the code below.
print((lambda x: x*x)(10))
Output:
100
This will again give the same result as expected. We just created the lambda function and directly call that in the code.
2.2 Lambda function with conditionals
Suppose we want to create a function to check whether the sum of two numbers is odd or even. Normally our function looks something like this below.
def oddEven(x,y):
if (x+y)%2==0:
print('sum is even')
else:
print('sum is odd')
oddEven(1,4)
oddEven(5,5)
Output:
sum is odd
sum is even
Let see how can we do all of the above using lambda function.
oddEven = lambda x,y: 'sum is odd' if (x+y)%2 else 'sum is even'
print(oddEven(1,4))
print(oddEven(5,5))
Output:
sum is odd
sum is even
In the above Lambda function, we have used lambda
as the keyword to show that it is a lambda function, x,y
as arguments and an expression ‘sum is odd’ if (x+y)%2
else ‘sum is even’, This expression will return ‘sum is odd’ if (x+y)%2
evaluates to be true that is if it is not 0, else it will return ‘sum is even’.
2.3 Lambda function with built-in map() function.
In Python, the map()
function takes a function and an iterable as arguments and returns a list after applying the function to all the elements of the iterable. Let’s see a simple use case of the map()
function that returns the square of all elements passed as a list.
def square(x):
return x*x
l = [2,4,5,6,7]
print(list(map(square,l)))
Output:
[4, 16, 25, 36, 49]
In the above code, each element of list l is passed to function square and the result is stored as a list. Now take a look at the below implementation of the same code using lambda function.
l = [2,4,5,6,7]
print(list(map(lambda x:x*x ,l)))
Output:
[4, 16, 25, 36, 49]
2.4 Lambda function with built-in filter() function.
In Python, the filter()
function takes a function and an iterable as arguments and returns a list of all elements from the iterable passed which evaluates to be true after applying the function. Let’s see a simple use case of filter() function that returns the list of all numbers greater than 4. Like always we will look at the approach without using a lambda function
def great4(x):
return x>4
l = [2,4,5,6,7]
print(list(filter(great4 ,l)))
Output:
[5, 6, 7]
So we got the list of all elements that are greater than 4 from the input list. Let’s have a look at the code using the lambda function.
l = [2,4,5,6,7]
print(list(filter(lambda x:x>4 ,l)))
Output:
[5, 6, 7]
Now we have enough understanding of how we can use the python lambda function in our code to make it more concise and highly suitable when we want a function for one-time use in the program.
3. References
Happy Learning 🙂