Skip to main content

Think of a lambda function as a small function with no name.

It may take as many arguments as you like, but its body is limited to one expression.


Syntax

Python evaluates the expression and returns the result.

Example

Return argument a plus 10:

Prompt
Write a Python program that creates a lambda function which adds 10 to its argument. Call the lambda with the value 5 and print the result.
Output: complete Python program

Example

Return the product of arguments a and b:

Prompt
Write a Python program that creates a lambda function which multiplies two arguments together. Call the lambda with 5 and 6 and print the result.
Output: complete Python program

Example

Return the sum of arguments a, b and c:

Prompt
Write a Python program that creates a lambda function which adds three arguments together. Call the lambda with 5, 6, and 2, and print the result.
Output: complete Python program
main.py
lambda arguments : expression

Why Use Lambda Functions?

Lambdas are at their best when used as an anonymous function inside another function.

Picture a function with one parameter, where the lambda it returns multiplies a number you haven't supplied yet by that parameter.

Example

Prompt
Write a Python program that defines a function myfunc(n) which returns a lambda that multiplies its argument by n. Use myfunc to create a doubler function (n=2), then print the result of calling it with 11.
Output: complete Python program

Example

Prompt
Write a Python program that defines a function myfunc(n) which returns a lambda that multiplies its argument by n. Use myfunc to create a tripler function (n=3), then print the result of calling it with 11.
Output: complete Python program

Example

Prompt
Write a Python program that defines a function myfunc(n) which returns a lambda that multiplies its argument by n. Create both a doubler (n=2) and tripler (n=3), then print the results of calling each with 11.
Output: complete Python program

Lambda with Built-in Functions

You will often see lambda functions passed to built-ins such as map(), filter() and sorted().

Example

Multiply every number in a list by two:

Prompt
Write a Python program that uses map() with a lambda to double all numbers in the list [1, 2, 3, 4, 5]. Print the resulting list.
Output: complete Python program

Example

Keep only the odd numbers from a list:

Prompt
Write a Python program that uses filter() with a lambda to get all odd numbers from [1, 2, 3, 4, 5, 6, 7, 8]. Print the resulting list.
Output: complete Python program

Example

Order a list of tuples by their second element:

Prompt
Write a Python program that sorts a list of tuples [("Emil", 25), ("Tobias", 22), ("Linus", 28)] by the second element using sorted() with a lambda key. Print the sorted list.
Output: complete Python program

Example

Order strings from shortest to longest:

Prompt
Write a Python program that sorts the list ["apple", "pie", "banana", "cherry"] by string length using sorted() with a lambda key. Print the sorted list.
Output: complete Python program