Back to Course

Intermediate Python

0% Complete
0/0 Steps
Lesson 11 of 33
In Progress

Lambda Functions

Lambda functions, also known as anonymous functions, are small functions that are defined without a name. They are often used when a function is required for a short period of time, or as an argument to another function.

Using Lambda Function

To create a lambda function, you use the lambda keyword, followed by a list of arguments, a colon, and the function body. The lambda function is then assigned to a variable or passed to another function as needed. For example:

add = lambda x, y: x + y
print(add(3, 4))  # Output: 7

numbers = [1, 2, 3, 4, 5]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # Output: [2, 4]

Lambda functions are often used in conjunction with higher-order functions, such as map(), filter(), and reduce(), which take other functions as arguments. For example:

numbers = [1, 2, 3, 4, 5]
doubles = list(map(lambda x: x * 2, numbers))
print(doubles)  # Output: [2, 4, 6, 8, 10]

words = ['cat', 'window', 'defenestrate']
lengths = list(map(lambda x: len(x), words))
print(lengths)  # Output: [3, 6, 12]

from functools import reduce
sum = reduce(lambda x, y: x + y, numbers)
print(sum)  # Output: 15

Lambda functions can also be used as the key argument in the sorted() function to sort a list based on a custom sorting criteria. For example:

words = ['cat', 'window', 'defenestrate']
sorted_words = sorted(words, key=lambda x: len(x))
print(sorted_words)  # Output: ['cat', 'window', 'defenestrate']

Conclusion

Lambda functions are a concise and convenient way to define a simple function in Python, but they are limited in their functionality. They can only contain a single expression, and cannot contain statements or annotations. If a lambda function requires more complexity, it is usually better to define a regular function using the def keyword.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Use a lambda function to sort a list of tuples based on the second element in the tuple.

data = [('Alice', 20), ('Bob', 25), ('Charlie', 15)]
data.sort(key=lambda x: x[1])
print(data)  # Output: [('Charlie', 15), ('Alice', 20), ('Bob', 25)]

Use a lambda function to filter a list of strings based on their length.

words = ['cat', 'window', 'defenestrate']
long_words = list(filter(lambda x: len(x) > 6, words))
print(long_words)  # Output: ['defenestrate']

Use a lambda function to map a list of strings to their upper case versions.

words = ['cat', 'window', 'defenestrate']
upper_words = list(map(lambda x: x.upper(), words))
print(upper_words)  # Output: ['CAT', 'WINDOW', 'DEFENESTRATE']

Use a lambda function as the key argument in the min() function to find the shortest string in a list.

words = ['cat', 'window', 'defenestrate']
shortest = min(words, key=lambda x: len(x))
print(shortest)  # Output: 'cat'

Use a lambda function as the key argument in the max() function to find the longest string in a list.

words = ['cat', 'window', 'defenestrate']
longest = max(words, key=lambda x: len(x))
print(longest)  # Output: 'defenestrate'