Lambda (Python)

A lambda is a small, anonymous function written in a single line. It is handy for short throwaway functions, often passed to sorted(), map(), or filter().

Python

Syntax

lambda arguments: expression

Examples

Python

double = lambda x: x * 2
print(double(5))

Output:

10

Python

pairs = [(1, "b"), (2, "a")]
pairs.sort(key=lambda p: p[1])
print(pairs)

Output:

[(2, 'a'), (1, 'b')]

Learn it in a lesson

See also

Back to the Dictionary