We have seen how to map a function across a list of data, with the return value of each function call placed into a list of results. For example, you summed together two lists of numbers using map
using code such as this. In the Python Console type:
def add(x, y):
"""Function to return the sum of x and y"""
return x + y
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
result = map(add, a, b)
list(result)
This returns a list of results. However, what if we want to sum every value in the returned list of results to form a single value? We could write the code by hand, e.g.:
total = 0
result = map(add, a, b)
for i in result:
total += i
print("Total = %s" % total)
This process of summing a list of numbers into a total is an example of “reduction”. The list of numbers has been reduced to a total by adding each value onto a running total. While mapping takes N elements and creates N new elements, a reduction takes N elements and returns 1 element. Reduction is the complement to mapping, and as such, Python has a reduce
function.
The reduce
function is available from the standard functools
module:
from functools import reduce
result = map(add, a, b)
reduce(add, result)
reduce
takes two required arguments and one additional, optional argument:
add
takes two arguments and returns the sum of those arguments. This can be any function that accepts two arguments and returns a single result.For example, type
a = [1, 2, 3, 4, 5]
reduce(add, a, 10)
Why do you think the answer is 25
?
Python’s reduce
applies the reduction function (in this case add
) cumulatively from left to right along the items of a list. If an initial value is supplied then this is used as the first value. Otherwise, the first value is the result of the reduction function applied to the first two items in the list. In the above case, reduce performed:
total = 10
total = add(total, 1)
total = add(total, 2)
total = add(total, 3)
total = add(total, 4)
total = add(total, 5)
The result is thus 25
, i.e. $(((((10+1)+2)+3)+4)+5)$.
The reduction function can be any function that accepts two arguments and returns a single value. For example, let’s now use reduce
to calculate the product of all of the values in the list. To do this, we need to create a new function that will take in two arguments and return their product:
def multiply(x, y):
"""Return the product of the two arguments"""
return x * y
reduce(multiply, a)
You should see that the product is 120
. Is this what you expected? In this case, reduce
performed;
total = multiply(1, 2)
total = multiply(total, 3)
total = multiply(total, 4)
total = multiply(total, 5)
i.e. it set total
equal to $((((1×2)×3)×4)×5) = 120$.
Note that the reduction function is not limited to just numbers. You can write a reduction function to reduce any types of object together. For example, we could use reduce to join together some strings:
def join_strings(x, y):
return f"{x} {y}"
a = ["cat", "dog", "mouse", "fish"]
reduce(join_strings, a)