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

list(map(lambda x: x**2,[1,2,3,4,5]))

Output:

[1, 4, 9, 16, 25]

Filter out odd element, only keep even element of a list

list(filter(lambda x: x%2==0, [1,2,3,4,5]))

It output below:

[2,4]

Last updated