Kodeclik Blog

Python Reduce Function

Like the Python map function, Python’s reduce function is easy to motivate. One key difference is that while map is built-in into Python, to get the reduce functionality you will need to import a Python module, like functools.

Summing numbers in a list with reduce()

Let us consider a simple application. Assume we want to sum the numbers in a list. We can write a simple for loop such as:
This produces the output:
This task can be elegantly achieved using Python reduce. Here is how that would work.
The output is:
Note that we are passing two arguments to reduce(). The first is the function sum which adds two numbers. The second is the list whose elements we desire to add. Even though sum takes only two elements, reduce() applies it incrementally over the full list, accumulating the partial sums, and finally arriving at the answer of 20.
Let us try to use reduce with an empty list:
This produces:
Woah! What happened? As the error message indicates, given an empty list, the reduce() function is not sure what to output because you haven’t specified any initial value. You should use a third argument to specify an initial, default, value. Consider the following program:
This outputs:
In both applications of reduce() above, just for illustration purposes, we use 3 as the default, initial, value. This is why with an empty list, we get 3 as the answer. With the list mylist, 3 is added to the sum of the elements in the list, giving 23 (instead of 20).

Finding the maximum in a list with reduce()

Let us adapt the same concepts and use reduce() to find the maximum elements in a list. Remember that the function that is used as argument to reduce() takes just two arguments. In our case, this must be a function that takes two elements and returns the greater element.
This outputs:
Note that, again, this will not work if you give an empty list. You will need to adopt a “default” convention.

Summing squares of numbers using reduce()

Let us use reduce() to take a list of numbers and find the sum of squares of the numbers in the list. Here’s how it would work. You first write a function that takes two numbers and outputs the sum of squares of just these two numbers. Then you use reduce() to apply it to the full list. Here is how that might work:
This outputs:
Woah! Something went wrong. The correct answer should be 30. Let us try to debug it. Let us try it with just a singleton element list:
This outputs:
This is still incorrect! What is happening?
The reason this is happening is because when you give a list containing non-zero elements, the first element’s value is used as the initial setting. Then the function is iterated over the remaining items in the list. In this case, there are no other items, so the function exits. As a result, you obtain the result 5. We should instead specify an initial value of 0:
This works. It outputs:
as expected. Let us try it with 2 elements.
This outputs (as expected):
Let us now try it with 3 elements:
This outputs:
Aha - this is where things go wrong! The reason you get 34 is that after computing the partial sum of 5, the sum_squares() function is called with arguments 5 and the remaining element in the list, which is 3. As a result you get 25+9=34.
So we really need to write our function as follows:
We get the output we expected:
Try it also with an empty list, singleton list, and so on.
If you have seen our blogpost on Python's map() function, you can combine that with reduce to directly take a list of numbers, square each of them, and then sum them up like so:
This outputs, again:
In more detail, the above code first uses the map() function to apply the square() function on each element of the list. This yields another list which is used as the second argument to the reduce() function. The first argument to reduce() is the sum() function.

Using Python reduce to compute the factorial

Here is a program that uses reduce() to compute the factorial of a number.
Note that we are using a lambda function and the second argument to the reduce() function is a list of numbers from 1 to the number we wish to compute the factorial of (which is “n”). Recall that we need to use n+1 as the argument to range because the end range parameter is not inclusive. This program outputs 10! As expected:
So what have we learnt in this blog post? The reduce() function is a way to distill all the elements of an iterable (like a list) into a single value by repeatedly applying a function, and aggregating the partial results into the final answer. If you liked this blogpost, learn also about Python's sum() function and the Python range() function. Closely related is Python numpy's sum() function. Also see how to use these concepts to flatten a nested list in Python.
Interested in more things Python? See our blogposts on Python's enumerate() and Python pointers. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.