Skip to content
Justice Tech School

Lambda Functions

A lambda is a one-line function without a name: lambda arguments: expression. It is mostly used as a short argument to functions such as sorted, map and filter. If you need more than one expression, write a normal def.

Example
double = lambda x: x * 2
print(double(6))
add = lambda a, b: a + b
print(add(2, 3))
words = ["banana", "fig", "apple"]
print(sorted(words, key=lambda w: len(w)))
print(list(map(lambda n: n + 1, [1, 2, 3])))
print(list(filter(lambda n: n > 1, [1, 2, 3])))

Result

12
5
['fig', 'apple', 'banana']
[2, 3, 4]
[2, 3]

Exercise

Create a lambda.

square =  x: x * x

Quiz Test

A lambda can contain...

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.