This is a detailed tutorial of Python Lambda. Learn how to take arguments and make use of expressions to return values using this python anonymous Function.
Python Lambda Function
It is a small function that is anonymous in nature. Any number of arguments can be given to a Lambda Function but it returns the value only according to one defined expression. Lambda Functions are important as they are anonymous and can be used within other user-defined Python Functions for better productivity. We’ll find it out later in this article.
Let’s first have a look at the basic syntax of a Lambda Function to understand how it works.
result = lambda args : expression
The name of the function is the variable name that is being used to store the return value of the lambda function. Therefore, the variable
result is the name of the lambda function itself in the above syntax. The function definition starts with the keyword lambda followed by the arguments and then the expression is written after a semi-colon.
The value of the expression is calculated as per the provided arguments and accordingly, the resultant value is returned.
Lambda functions are helpful in different ways. Let’s have a look at the different examples given below. We’ve tried to include each different example to explain different practical usage.
Single Parameter Lambda Function
Example. In the following code, we’ve written a very simple lambda function that calculates the square of a number and returns it.
#Lambda Function Definition
square = lambda par : par * par
#Calling a Lambda Function
In this lambda function, we’ve defined the function to accept only one parameter, i.e. par and then in the expression we’re simply multiplying this parameter twice so that it can return the square of it. Afterward, we’re simply calling the lambda function by passing the argument like normal Python Functions.
Lambda Function with Multiple Parameters
Example. This demonstrates how you can accept multiple arguments using a Lambda Function.
#Lambda Function with Two Parameters
area = lambda length, breadth : length * breadth
#Calling To Calculate Area
This lambda function takes two arguments, length and breadth and by multiplying both of these it calculates the area and returns it as shown in the following screenshot.
Lambda Function For Mapping inside the
Example. In the following code snippet, we’re mapping a list in such a way that it makes use of the anonymity of the lambda function to multiply each element of the list by a number 5 and returning a new Python List. You can know more about the usage of Python Map Function here.
numbers = [1,2,3,4,5,6,7,8,9,10]
multiplyNumbers = list(map(lambda x : x * 5, numbers))
x of the lambda function here is being passed via the
map() function, for each element of the list.
This way you can also use the lambda function inside functions like
filter() as well.
Lambda Functions Inside Other Python Functions
To better use the anonymous feature of the lambda function, these are often used inside the normal python functions. Let’s find out how they are more beneficial to use inside another function.
Example 1. Here we’re calculating the multiplication table of a number provided as an argument using the lambda function and the map() function inside a user-defined function.
return list(map(lambda x : x * number, range(0,11)))
Example 2. Here, we’re using the anonymous nature of the lambda function even better. Observe the code and the output carefully to better understad how it’s working.
return lambda x : x * multiplier
double = applyMultiplier(2)
triple = applyMultiplier(3)
I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!