The art and Tao of Python Programming: an introduction to Python language
Link to the video course "the art and Tao of Python Programming: an introduction to Python language": https://edu.csdn.net/course/detail/27845
Lambda function
Lambda function is an in-line function without a name. It is often called an anonymous function.
The function of lambda function is exactly the same as that of ordinary functions, except that lambda contains only one expression
Basic Example
def doubler(x): return x*2 print(doubler(2)) # 4 print(doubler(5)) # 10
4 10
You can use the lambda function to get the same result.
doubler = lambda x: x*2 print(doubler(2)) # 4 print(doubler(5)) # 10
4 10
Multiple parameters
You can send any number of arguments to the lambda function. Just separate them with commas.
Example: lambda function that multiplies two values
mul = lambda x, y: x*y print(mul(2, 5))
10
Example: a lambda function that adds three values
add = lambda x, y, z: x+y+z print(add(2, 5, 10))
17
Default parameter value
You can specify default values for parameters.
When you call a lambda function without parameters, it uses the default value.
Example: lambda function that increments the value by 1 by default
incrementer = lambda x, y=1: x+y print(incrementer(5, 3))
8
# using default print(incrementer(5))
6
Return multiple values
To return multiple values, package them in a tuple.
Multiple assignments are then used to unpack the parts of the tuple returned by the package.
Example: return multiple values by packaging them into a tuple
findSquareCube = lambda num: (num**2, num**3) print(type(findSquareCube)) x, y = findSquareCube(2) print(x) print(y)
<class 'function'> 4 8
if else in a Lambda
To implement selection logic in lambda, you can use an if else ternary expression.
Example: lambda function that returns the smallest element
findMin = lambda x, y: x if x < y else y print(findMin(2, 4)) # 2 print(findMin('a', 'x')) # a
2 a
List derivation in Lambda
You can use list derivation in lambda functions.
Example: flattening a nested list using lambda
flatten = lambda l: [item for sublist in l for item in sublist] L = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] print(flatten(L)) # [1, 2, 3, 4, 5, 6, 7, 8, 9] L = [['a', 'b', 'c'], ['d', 'e']] print(flatten(L)) # ['a', 'b', 'c', 'd', 'e']
[1, 2, 3, 4, 5, 6, 7, 8, 9] ['a', 'b', 'c', 'd', 'e']
Sorting with lambda
The Lambda function can be used with the sorted() function to sort iteratable objects.
Example: sort students by age
L = [('Sam', 35), ('Max', 25), ('Bob', 30)] x = sorted(L, key=lambda student: student[1]) print(x) # [('Max', 25), ('Bob', 30), ('Sam', 35)]
[('Max', 25), ('Bob', 30), ('Sam', 35)]
Nested Lambdas
Like ordinary functions, lambda can also be nested.
Example: nested Lambda
multiplier = (lambda x: (lambda y: x*2 + y)) doubler = multiplier(2) print(doubler(10)) # 14 tripler = multiplier(3) print(tripler(10)) # 16
14 16
Jump Tables
lambda is also commonly used to implement jump tables.
A jump table is a list or dictionary of functions to be called on demand.
Example: create a jump table for square sum and cube operations in python
# dictionary of functions exponent = {'square':lambda x: x ** 2, 'cube':lambda x: x ** 3} print(exponent['square'](3)) # 9 print(exponent['cube'](3)) # 27 # list of functions exponent = [lambda x: x ** 2, lambda x: x ** 3] print(exponent[0](3)) # 9 print(exponent[1](3)) # 27
9 27 9 27