lambda

lambda

lambda functions are nameless function. lambda functions are usually one line functions. lambda functions are usually used as input parameter to map and filter function.
For example:
Make every element of list [1,2,3,4,5] square
1
list(map(lambda x: x**2,[1,2,3,4,5]))
Copied!
Output:
[1, 4, 9, 16, 25]
Filter out odd element, only keep even element of a list
1
list(filter(lambda x: x%2==0, [1,2,3,4,5]))
Copied!
It output below:
[2,4]
Last modified 1yr ago
Copy link