Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (27 sloc) 869 Bytes


Lambdas are one line functions. They are also known as anonymous functions in some other languages. You might want to use lambdas when you don't want to use a function twice in a program. They are just like normal functions and even behave like them.


lambda argument: manipulate(argument)


add = lambda x, y: x + y

print(add(3, 5))
# Output: 8

Here are a few useful use cases for lambdas and just a few ways in which they are used in the wild:

List sorting

a = [(1, 2), (4, 1), (9, 10), (13, -3)]
a.sort(key=lambda x: x[1])

# Output: [(13, -3), (4, 1), (1, 2), (9, 10)]

Parallel sorting of lists

data = zip(list1, list2)
list1, list2 = map(lambda t: list(t), zip(*data))