Welcome to this lesson on combining functions in Python! By now, you should already be familiar with map
, filter
, reduce
, and sorted
from our previous lessons. Combining these functions lets you perform powerful data transformations concisely and readably.
By the end of this lesson, you'll understand how to combine multiple higher-order functions to perform complex data processing tasks. Specifically, we'll cover how to:
- Use
map
,filter
, andreduce
together to perform cumulative operations on filtered data. - Combine
map
andsorted
to achieve custom ordering of processed data.
These skills will help you write more efficient and readable code.
Let's solve a simple problem: finding the sum of the squares of even numbers from a list. We'll break this into three steps:
- Filtering the even numbers.
- Squaring these even numbers.
- Summing the squared values.
We'll use Python's filter
, map
, and reduce
functions.
Use the filter
function to extract even numbers. filter
takes two arguments: a function and an iterable. It only keeps items for which the function returns True
.
Example:
Python1numbers = [5, 1, 9, 3, 7, 8, 6, 2, 4, 0] 2 3# Define a lambda function to check even numbers 4is_even = lambda x: x % 2 == 0 5 6# Use filter to extract even numbers 7even_numbers = list(filter(is_even, numbers)) 8print(even_numbers) # [8, 6, 2, 4, 0]
Use the map
function to square each filtered even number. map
also takes two arguments: a function and an iterable.
Example:
Python1# Define a lambda function to square numbers 2square = lambda x: x ** 2 3 4# Use map to square even numbers 5squared_numbers = list(map(square, even_numbers)) 6print(squared_numbers) # [64, 36, 4, 16, 0]
Finally, use the reduce
function from the functools
module to sum the squared values. reduce
takes a function and an iterable, and applies the function cumulatively.
Example:
Python1from functools import reduce 2 3# Define a lambda function to sum numbers 4sum_numbers = lambda acc, x: acc + x 5 6# Use reduce to sum the squared numbers 7sum_of_squares = reduce(sum_numbers, squared_numbers) 8print(sum_of_squares) # 120
The beauty of functional programming shines when you combine all these steps into one streamlined operation.
Python1from functools import reduce 2 3# Define a list of numbers 4numbers = [5, 1, 9, 3, 7, 8, 6, 2, 4, 0] 5 6# Combine map, filter, and reduce to find the sum of squares of even numbers 7sum_of_squares_of_evens = reduce( 8 lambda acc, x: acc + x, 9 map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)) 10) 11print("Sum of squares of even numbers:", sum_of_squares_of_evens) # Sum of squares of even numbers: 120
This code is concise and efficient. The nested function calls allow you to keep the program short.
Let's sort the squared values of even numbers in descending order. Combine filtering, mapping, and sorting operations.
The sorted
function allows custom sorting criteria. To sort in descending order, use the reverse=True
parameter.
Example:
Python1# Define a list of numbers 2numbers = [5, 1, 9, 3, 7, 8, 6, 2, 4, 0] 3 4# Use sorted to sort squared even numbers in descending order 5sorted_squares_of_evens = sorted( 6 map(lambda x: x ** 2, filter(lambda x: x % 2 == 0, numbers)), 7 reverse=True 8) 9print("Sorted squares of even numbers (descending):", sorted_squares_of_evens) # Sorted squares of even numbers (descending): [64, 36, 16, 4, 0]
Congratulations! You've learned how to combine higher-order functions in Python to perform complex data transformations. Here's a quick summary of what we've covered:
- Combining filter, map, and reduce: We filtered even numbers, squared them, and then summed the squared values.
- Combining with sorting: We sorted the squared even numbers in descending order.
These combinations let you handle diverse data processing tasks efficiently.
Now it's time to apply your knowledge in hands-on exercises. You'll get to combine these functions and tackle various data processing challenges. Happy coding!