How to create Lambda Expressions
Lambda Expressions
lambda expressions are small, unnamed functions that can only contain an expression, and return its value. A lambda expression is written like this:
lambda x, y, z: x + y + z
The first word, lambda, is a reserved word. It is followed by the parameters, a colon ( :), and the body.
bigger = lambda a, b : a > b
print bigger(1,2)
print bigger(2,1)
The code above generates the following result.
The map function "maps" one sequence into another by applying a function to each of the elements.
For example, you may have a list of numbers, and you want to create another list in which all the numbers are doubled:
numbers = [2, 1, 10, 8, 11]
map(lambda n: 2*n, numbers)
lambda function as a list number
L = [(lambda x: x**2), (lambda x: x**3), (lambda x: x**4)]
# from w w w .ja va 2 s. com
for f in L:
print f(2) # prints 4, 8, 16
print L[0](3) # prints 9
The code above generates the following result.