Functional programming is a programming paradigm that breaks down a problem into a set of individual functions. For example, the reduce() function helps us to a list into a single value. That’s how powerful the function reduce() is.
Python reduce
Python reduce() function applies a specific function passed in its argument to all list elements mentioned in the sequence. The reduce() function implements the mathematical procedure called folding or reduction.
The reduce() function works on any iterable and not just lists. Therefore, the reduce() method is beneficial when applying a function to an iterable and reducing it to a single cumulative value.
There is no built-in reduce() function in Python programming. To implement the reduce() function in Python, import the functools module. The reduce() function is defined under the functools module, which is used to apply a specific function passed in its argument to all of the iterable items mentioned in the sequence passed along.
How does the reduce() function works in Python?
The reduce() function performs the following steps.
- Apply a function (or callable) to the first two elements in the iterable and generate a temporary result.
- Use that quick result and the third item in the iterable to generate another temporary result.
- Repeat the process until the iterable is depleted and then return a final single cumulative value.
Syntax
functools.reduce(function, iterable[, initializer])
Parameters
The reduce() function takes two required parameters and one optional argument.
The first parameter is a function that will take two values from the iterable and generate a temporary result.
The second parameter is iterable, which can be a list, dictionary, tuple, or other iterables.
The third parameter is optional and takes an initial value that can be processed in the function.
To understand how the reduce() function works, you will write a function that computes the sum of two numbers and prints the equivalent math operation to the screen. Here’s your code.
# app.py def addition(x, y): result = x + y print(f"{x} + {y} = {result}") return result addition(11, 19)
Output
python3 app.py 11 + 19 = 30
The addition() function calculates the sum of x and y, prints a message with the operation using an f-string, and returns the result of the cumulative calculations.
Check out the following code that uses a list of numbers, an addition() function, and a reduce() function that returns a single value.
# app.py from functools import reduce numbers = [11, 19, 21, 18, 46] def addition(x, y): result = x + y return result print(reduce(addition, numbers))
Output
115
You can see that the list of numbers reduces to a single value, which is the addition of all the list items.
When you call reduce() function, passing the addition() function and numbers as arguments, you get an output that shows all the computations that reduce() method performs to reduce it to a single value of 115. In this case, the operations are equivalent to (((((0 + 11) + 19) + 21) + 18) + 46) = 115.
Passing initializer argument
In the above example, we did not pass the initializer argument, which is optional. But let’s give this example. First, we pass 29 as an initial argument and see how it affects our previous output.
# app.py from functools import reduce numbers = [11, 19, 21, 18, 46] def addition(a, b): result = a + b return result print(reduce(addition, numbers, 29))
Output
144
If you pass the initializer value, then reduce() function, feed it to the first call of function as its first argument.
This means that the first call to the function will use the value of the initializer and the first item of iterable to perform its first temporary computation.
After this, the reduce() function continues working with the following items of iterable.
If you pass the value to the initializer, the reduce() function will perform one more iteration than it would without an initializer.
Conclusion
Python functools.reduce() is a valuable programming function in which iterator values are transformed into a single value using mathematical computation. That’s it for this tutorial.
One of the interesting fact is reduce works with one element iterable as well.