Lambda functions are special types of functions that are not expressed as regular functions. Lambda functions do not have any function name to call and they are created for single line simple functions. The lambda function will eliminate the
def keyword for a function definition.
Lambda functions are also called a small anonymous function where it does not have any function name. In the following part of the tutorial, we will see that lambda functions can take different types of arguments.
Lambda Function Use Characteristics and Use Cases
- As a single line function definition it only contains expressions and does not contain any statements in its body.
- It is written as a single line of execution
- Lambda functions are easy to read because of its simplicity and atomicity.
- Lambda functions do not support type annotations.
- Lambda functions can be immediately called after definition by adding parameters in the same line.
Syntax of Lambda
The syntax of the Lambda function definition is very different than the other Python keywords and structures.
VAR = lambda ARG1, ARG2, ... : EXPRESSION
- `VAR` is the variable name where the lambda function will be assigned.
- `lambda` is the keyword used to define and create a lambda function
- `ARG1` ,`ARG2` , … are arguments for the lambda function. We can use a single argument or multiple arguments. As they are optional we can also provide zero argument.
- `EXPRESSION` ist the body of the lambda function which will be executed every time the lambda function is called
We will start with a simple example where we will provide a single argument to the lambda function. We will provide the argument name as
a and the lambda function will be assigned to the
x = lambda a: "This is the "+a print(x("poftut.com")) x = lambda a: a*a*a print(x(3)) print(x(4))
- In the first example, we will provide the “poftut.com” to the lambda function and the function will return the string “This is the poftut.com”.
- In the second example, we provide 3 to the lambda function and it is multiplied 3 times with itself.
- In the second example, we provide 4 to the lambda function and it is multiplied 3 times with itself.
Lambda without Argument
We can use the lambda function without providing any argument. We will just put the expression part of the lambda function and use the newly created Lambda function. In this example, we will create the lambda function and assign it to the
x where the function will print “This is the X” to the screen.
x = lambda : "This is the X" print(x())
Call Lambda Function Directly
Lambda function can be also called directly without assigning it to a variable. We will just provide the parameters after the lambda function definition. We will also surround the lambda function and provided arguments with brackets. In the following examples, we will do math and print “This site is poftut.com”.
(lambda x: x + 1)(2) (lambda x: x * x + 1)(3) (lambda x: "This site is "+x)("poftut.com")
Lambda with Multiple Arguments
Lambda functions can be used without argument or a single argument or multiple arguments. We can provide multiple arguments in a row which is also called positional arguments. In this example, we will provide arguments a as 1 , b as 2 and c as 5 like below.
x = lambda a, b, c: a + b + c print(x(1, 2, 5))
Lambda with Named Arguments
We can use named arguments in lambda functions. We will set the
c value as 5 by default. If the c is not provided to the lambda function call it will be assumed as 5. If provided the provided new value will be used.
x = lambda a, b, c=5 : a + b + c print(x(1, 2)) print(x(10, 20)) print(x(1, 2, 10))
Lambda with Variable List Of Arguments
If the provided arguments list and the count are variable we have to use
args keyword which can map variable-length arguments. We will use
*args as variable-length arguments and use inside the expression.
x = lambda *args : sum(args) x(1,2,3) x(1) x(1,2) x(1,2,3,4) x(1,2,3,4,5) x(1,2,3,4,5,6) x(1,2,3,4,5,6,7) x(1,2,3,4,5,6,7,8) x(1,2,3,4,5,6,7,8,9)
Lambda with Variable List Of Keyword Arguments
We can also use a variable list of keywords inside the Lambda functions. We will use
kwargs keyword and select their values with the
values() function. We will provide the arguments in different names.
x = lambda **kwargs : sum(kwargs.values()) x(a=1 , b=2 ) x(a=1 , b=2 , c=3 ) x(a=1 , b=2 , c=3 , d=4 ) x(a=1 , b=2 , c=3 , d=4 , e=5 ) x(a=1 , b=2 , c=3 , d=4 , e=5 , f=6 )
Lambda with Python Interactive Shell
Lambda provides special usage with the Python interactive shell. We can create a lambda function without a variable assignment and call it later with the underscore
_ operator. But we can only call the lambda function for one time after definition. If we try to call the lambda function the second time after the definition we will get an exception.
>>> lambda x: "This site is "+x >>> _("POFTUT.com") >>> lambda x: x * x + 1 >>> _(3) >>> _(4) >>> lambda x: x * x + 1 >>> _(4)
Lambda with filter() Function
filter() function is used to filter a sequence of variables according to the situation which is defined in a function. Lambda functions are very useful to create a situation for filtering. Using the lambda function will also make the filter more readable.
mynumbers = [ 2 , 4 , 5 , 34 , 245 , 56 , 4 , 7 , 8 , 0 , 45 ] filtered_numbers = list(filter(lambda x: (x%2 !=0), mynumbers)) print(filtered_numbers)
Lambda with map() Function
map() function is used to run a given function on the given list elements one by one. Lambda function another popular use case is using it with given list elements by using map() function. In this example, we will use lambda function in order to calculate square of the given list elements one by one.
mynumbers = [ 2 , 4 , 5 , 34 , 245 , 56 , 4 , 7 , 8 , 0 , 45 ] filtered_numbers = list(map(lambda x: x * x , mynumbers)) print(filtered_numbers)
Lambda with reduce() Function
reduce() is another interesting function wherefrom the given list multiple elements can be processed by the given function. As a function lambda can be used very efficiently for simple operations. In this example, we will sum for 2 neighbor elements in each step or reduce.
mynumbers = [ 2 , 4 , 5 , 34 , 245 , 56 , 4 , 7 , 8 , 0 , 45 ] filtered_numbers = reduce(lambda x, y: x+y , mynumbers) print(filtered_numbers)