Peer uest
Contact Help
Home
 

A lambda function is a anonymous function which can only have one expression. 

Basic definition

A lambda function that takes three argument a, b, c and multiply them can be written as

func = lambda a, b, c : a * b * c
print(func(3, 5, 6))

The out put will be 14.

Use lambda with map()

The map() function takes in a function and a list as argument. The function will be applied to each element of the list. Then the new list is returned. Example

x = [1, 3, 5, 7]
x_square = list(map(lambda a : a * a), x)
print(x_square)

The output

[1, 9, 25, 49]

Note in Python 3+, map function return an iterator. To convert the iterator to a list, we need to apply the list function.

Add a Comment
×
×
×