Python lambda (Anonymous Function)
Python uses lambda to create anonymous functions.
A lambda function is a small, anonymous, inline function. It can have any number of arguments, but only one expression.
An anonymous function does not require using the def keyword to define a complete function.
Lambda functions are typically used to write simple, single-line functions, often used when a function needs to be passed as an argument, such as in functions like map(), filter(), reduce(), etc.
Lambda function features:
- Lambda functions are anonymous; they have no function name and can only be used by assigning them to a variable or passing them as arguments to other functions.
- Lambda functions usually contain only one line of code, making them suitable for writing simple functions.
Lambda syntax format:
lambda arguments: expression
lambdais Python's keyword for defining lambda functions.argumentsis the argument list, which can contain zero or more arguments, but must be specified before the colon (:).expressionis an expression used to compute and return the function's result.
The following lambda function takes no arguments:
Example
f
=
lambda
:
"
Hello, world!
"
(
f
(
)
)
Output: Hello, world!
The output is:
Hello, world!
The following example uses lambda to create an anonymous function, set a function parameter a, compute a plus 10, and return the result:
Example
x
=
lambda
a
:
a
10
(
x
(
5
)
)
The output is:
15
Lambda functions can also take multiple arguments, separated by commas ,:
The following example uses lambda to create an anonymous function, with function parameters a and b multiplied, and return the result:
Example
x
=
lambda
a
,
b
:
a
b
(
x
(
5
,
6
)
)
The output is:
30
The following example uses lambda to create an anonymous function, with function parameters a, b, and c added together, and return the result:
Example
x
=
lambda
a
,
b
,
c
:
a
b
c
(
x
(
5
,
6
,
2
)
)
The output is:
13
Lambda functions are often used together with built-in functions such as map(), filter(), and reduce() to perform operations on collections. For example:
Example
numbers
=
[
1
,
2
,
3
,
4
,
5
]
squared
=
list
(
map
(
lambda
x
:
x
**
2
,
numbers
)
)
(
squared
)
Output: [1, 4, 9, 16, 25]
The output is:
[1, 4, 9, 16, 25]
Use a lambda function together with filter() to filter even numbers:
Example
numbers
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
]
even_numbers
=
list
(
filter
(
lambda
x
:
x
%
2
==
0
,
numbers
)
)
(
even_numbers
)
Output: [2, 4, 6, 8]
The output is:
[2, 4, 6, 8]
The following demonstrates how to use reduce() and a lambda expression to compute the cumulative product of a sequence:
Example
from
functools
import
reduce
numbers
=
[
1
,
2
,
3
,
4
,
5
]
Use reduce() and a lambda function to compute the product
product
=
reduce
(
lambda
x
,
y
:
x
y
,
numbers
)
(
product
)
Output: 120
The output is:
120
In the example above, the reduce() function iterates through the numbers list and continuously updates the accumulated result using the lambda function, finally obtaining the result 1 * 2 * 3 * 4 * 5 = 120.