What is Lambda in Python: Lambda Function is also known as Anonymous Function in Python.
It is called Anonymous Function because unlike a Normal Function, the Lambda Function can be nameless as well.
Lambda Function is a compressed version of the normal or regular function and generally, the length of the lambda function is no more than 1 line.
Lambda function returns the function object which then can be assigned to any variable.
So, now that you have a basic understanding of What is lambda in Python.
Let’s move to our first topic which is the Syntax of Lambda Function.
Complete Guide on Lambda in Python
- 1. Syntax of Lambda Function in Python
- 2. Lambda vs Regular Function
- 3. Why use Lambda Function
- 4. Examples of Lambda Function in Python
- 4.1 Using Lambda Function inside another Function
- 4.2 Using Lambda Function for printing a string
- 4.3 Using multiple arguments with Lambda Function
- 5. filter() with lambda
- 6. map() with lambda
- 7. reduce() with lambda
Syntax of Lambda Function in Python
When it comes to a lambda function, it’s one of the most important features is its easy Syntax.
The syntax of the lambda function is as follow:
lambda arguments: expression
Here “lambda” is a reserved keywords in Python which is used for creating a lambda function.
Followed by the lambda keyword, you can define more than 1 argument, which you want to use in the function.
Then the colon symbol (:) is used as a separator between arguments and expression.
At last the expression is being evaluated which is also known as return statement of the lambda function.
However, make sure that in the lambda function you can use a single expression only.
Since the Lambda Function also returns a function object, so this whole lambda function can also be assigned to a variable.
That variable can be used as a function name as well.
variable = lambda arguments: expression
Here is a basic example of the lambda function, in which we are going to multiply a given number with 5.
x = lambda a: a * 5 print( x(4) )
So, as you can see that we have defined the lambda function using the lambda keyword and then we have used the argument “a” followed by the colon symbol and the return statement which is “a * 5”.
Then this whole lambda function is being returned to the variable named “x”.
In the next line, inside the print function, we have called the lambda function using the assigned variable (x) and the parameter (which is 4).
Now that we have seen our first example of the lambda function, its time to take a deep look at the difference between Regular Function and Lambda Function.
Lambda vs Regular Function
Understanding the lambda function becomes much easier if you already know the working of Regular or Normal Function.
Now we will understand it with the help of examples.
Firstly let’s take an example of a Regular Function and then we will replicate it with lambda function as well.
So, here is the code to return the half of any number passed as a parameter in the function.
def half(x): return x/2 print( half(14) )
Now, let’s create the same code with the help of the lambda function in Python.
half = lambda x: x / 2 print( half(14) )
As you can see that in the above code, we have defined the lambda function using the “lambda” keyword and we have used ‘x’ as an argument.
Then followed by colon sign, we have defined the return statement or expression which is ‘x / 2’.
Then we have assigned this function object to the variable named “half”.
At last, we have passed this half function with parameter 14 to the print function.
Now let’s see another example in which 2 numbers are passed in the argument and then the addition of those 2 numbers is being returned.
def add(a, b): return a + b print( add(6,7) )
Here is the equivalent lambda function of above code.
add = lambda a,b: a + b print( add(6,7) )
As we have already discussed that lambda function can have multiple arguments.
So, unlike previous examples we have discussed, in which a single argument is used, here we have used 2 arguments (a and b).
We can perform any operation inside the return statement or expression using these arguments.
Why use Lambda Function
There is several reasons why one should know the lambda function and implement it in the program as well.
Some of the most common reasons are as follow:
- Lambda Function makes your Code shorter.
- It helps in reducing the complexity of the program.
- It makes your code much faster and efficient.
- Using Lambda Function makes it easier for other developers to read your code.
Examples of Lambda Function in Python
Here are some more examples of the Lambda Function in Python:
1. Using Lambda Function inside another Function
def addition(num): return lambda a: a + num value1 = addition(15) value2 = addition(20) print( value1(5) ) print( value2(8) )
In the above program, we have used the lambda function inside the regular function.
2. Using Lambda Function for printing a string
s1 = "Hello there, This is the String" (lambda a: print(a))(s1)
Hello there, This is the String
In this program, we have used the lambda function for printing the string with the help of IIFE (Immediately Invoked Function Expressions).
3. Using multiple arguments with Lambda Function
addition = lambda a, b, c: a + b + c print( addition(5,6,7) )
Here is an example of using multiple arguments in the lambda function.
filter() with lambda
The filter() function in Python is used for filtering the elements from any list.
For example – if you want to filter or select even numbers from the given list, then you can make use of a filter().
The filter() take 2 parameters. The first one is the function, which will return the boolean value and the second one is the list of elements which has to be filtered.
The filter() function sequentially iterate over the list and return the element which fulfills the requirements provided by the function.
Below is the code for using filter() with lambda.
numbers = [6, 3, 2, 9, 12, 15, 11, 18] even_no = list(filter(lambda x: (x % 2) == 0, numbers)) print(even_no)
[6, 2, 12, 18]
Here in the first parameter, we have used the lambda function which has the boolean expression in the return statement.
In the second parameter, we have given the name of the list (numbers) which has to be filtered.
So, every time the element fulfill the requirements provided by the lambda function. The filter() function returns the element to our new list (which is even_no).
map() with lambda
The map() is one of the most resourceful and popular functions in Python.
It is used for performing a particular operation on every item of the list iteratively.
As you can see in the below code that we have used the map() function for multiplying every element of the list with 2.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] mul_no = list(map((lambda a: a * 2), numbers)) print(mul_no)
[2, 4, 6, 8, 10, 12, 14, 16, 18]
Here inside the map() function, 2 parameters are used.
The first one is the lambda function which has a single argument and in the expression, that argument is multiplied by 2 and in the second parameter, the name of the list is provided (which is numbers).
So, as we have discussed above, the map() function iterate over the “numbers” list and perform the operation provided in the lambda function iteratively.
After operating iteratively, it returns the evaluated value in our new list which is “mul_no”.
reduce() with lambda
The reduce() function does a similar job as map() do.
However, unlike the map() function, the reduce() function doesn’t return the list. Instead, it returns a single value or number.
The reduce() function takes a pair of elements from the list iteratively and performs the operation on it.
For example – Below is the code for adding all the elements of the list using a reduce() function.
from functools import reduce numbers = [1, 2, 3, 4, 5] mul_no = reduce((lambda a, b: a + b), numbers) print(mul_no)
So, as you can see that here we have used 2 arguments (a and b) in the lambda function, and reduce() function iteratively takes pair of values from the “numbers” list and provide it to the lambda function.
1 + 2 = 3 (Ist and IInd element is added)
3 + 3 = 6 (addition of Ist and IInd element is added with IIIrd element)
6 + 4 = 10 (addition of Ist, IInd and IIIrd element is added with IVth element)
10 + 5 = 15 (addition of Ist, IInd, IIIrd and IVth element is added with Vth element)
((((1 + 2) + 3) + 4) + 5)
As here first 1 and 2 are taken from the list and added with the help of the lambda function, which gives us 3.
Then again this 3 is added to the third element of the list and gives us 6.
Again 6 which is the addition of the Ist, IInd, and IIIrd element is added to the IVth element in the list.
This process goes on and on until the elements of the list get ended.
Hope you like the article on What is Lambda in Python. We also have an article on List Comprehension in Python which you must take a look at.