FANDOM


Lambda logo

A lambda function is a one line anonymous function that you don't plan on reusing. It can take any number of arguments, but can only have one expression.

SyntaxEdit

lambda arguments : expression

ExamplesEdit

1. A lambda function that adds 10 to the number passed in as an argument, and print the result.

x = lambda a : a + 10
print(x(5))

2. Multiplies argument a with argument b.

x = lambda a, b : a * b
print(x(5, 6))

3. Using lambda with the map function to square a list of numbers.

numbers = [4, 3, 6, 9, 11]
squared = list(map(lambda x: x**2, numbers))
print(squared)

# Returns:
[16, 9, 36, 81, 121]

ReferencesEdit

Python Lambda

Community content is available under CC-BY-SA unless otherwise noted.