Python's reduce(): From Functional to Pythonic Style

Python's reduce(): From Functional to Pythonic Style

Python’s reduce() is a function that implements a mathematical technique called folding or reduction. reduce() is useful when you need to apply a function to an iterable and reduce it to a single cumulative value. Python’s reduce() is popular among developers with a functional programming background, but Python has more to offer.

In this tutorial, you’ll cover how reduce() works and how to use it effectively. You’ll also cover some alternative Python tools that can be more Pythonic, readable, and efficient than reduce().

In this tutorial, you’ll learn:

  • How Python’s reduce() works
  • What the more common reduction use cases are
  • How to solve these use cases using reduce()
  • What alternative Python tools are available to solve these same use cases

With this knowledge, you’ll be able to decide which tools to use when it comes to solving reduction or folding problems in Python.

For a better understanding of Python’s reduce(), it would be helpful to have some previous knowledge of how to work with Python iterables, especially how to loop over them using a for loop.

Exploring Functional Programming in Python

Functional programming is a programming paradigm based on breaking down a problem into a set of individual functions. Ideally, every function only takes a set of input arguments and produces an output.

In functional programming, functions don’t have any internal state that affects the output that they produce for a given input. This means that anytime you call a function with the same set of input arguments, you’ll get the same result or output.

In a functional program, input data flows through a set of functions. Each function operates on its input and produces some output. Functional programming tries to avoid mutable data types and state changes as much as possible. It works with the data that flow between functions.

Other core features of functional programming include the following:

  • The use of recursion rather than loops or other structures as a primary flow control structure
  • A focus on lists or arrays processing
  • A focus on what is to be computed rather than on how to compute it
  • The use of pure functions that avoid side effects
  • The use of higher-order functions

There are several important concepts in this list. Here’s a closer look to some of them:

  • Recursion is a technique in which functions call themselves, either directly or indirectly, in order to loop. It allows a program to loop over data structures that have unknown or unpredictable lengths.

  • Pure functions are functions that have no side effects at all. In other words, they’re functions that do not update or modify any global variable, object, or data structure in the program. These functions produce an output that depends only on the input, which is closer to the concept of a mathematical function.

  • Higher-order functions are functions that operate on other functions by taking functions as arguments, returning functions, or both, as with Python decorators.

Since Python is a multi-paradigm programming language, it provides some tools that support a functional programming style:

Even though Python isn’t heavily influenced by functional programming languages, back in 1993 there was a clear demand for some of the functional programming features listed above.

In response, several functional tools were added to the language. According to Guido van Rossum, they were contributed by a community member:

Python acquired lambda, reduce(), filter() and map(), courtesy of (I believe) a Lisp hacker who missed them and submitted working patches. (Source)

Over the years, new features such as list comprehensions, generator expressions, and built-in functions like sum(), min(), max(), all(), and any() were viewed as Pythonic replacements for map(), filter(), and reduce(). Guido planned to remove map(), filter(), reduce(), and even lambda from the language in Python 3.

Luckily, this removal didn’t take effect, mainly because the Python community didn’t want to let go of such popular features. They’re still around and still widely used among developers with a strong functional programming background.

In this tutorial, you’ll cover how to use Python’s reduce() to process iterables and reduce them to a single cumulative value without using a for loop. You’ll also learn about some Python tools that you can use in place of reduce() to make your code more Pythonic, readable, and efficient.

Getting Started With Python’s reduce()

Python’s reduce() implements a mathematical technique commonly known as folding or reduction. You’re doing a fold or reduction when you reduce a list of items to a single cumulative value. Python’s reduce() operates on any iterable—not just lists—and performs the following steps:

  1. Apply a function (or callable) to the first two items in an iterable and generate a partial result.
  2. Use that partial result, together with the third item in the iterable, to generate another partial result.
  3. Repeat the process until the iterable is exhausted and then return a single cumulative value.

The idea behind Python’s reduce() is to take an existing function, apply it cumulatively to all the items in an iterable, and generate a single final value. In general, Python’s reduce() is handy for processing iterables without writing explicit for loops. Since reduce() is written in C, its internal loop can be faster than an explicit Python for loop.

Python’s reduce() was originally a built-in function (and still is in Python 2.x), but it was moved to functools.reduce() in Python 3.0. This decision was based on some possible performance and readability issues.

Another reason for moving reduce() to functools was the introduction of built-in functions like sum(), any(), all(), max(), min(), and len(), which provide more efficient, readable, and Pythonic ways of tackling common use cases for reduce(). You’ll learn how to use them in place of reduce() later in the tutorial.

In Python 3.x, if you need to use reduce(), then you first have to import the function into your current scope using an import statement in one of the following ways:

  1. import functools and then use fully-qualified names like functools.reduce().
  2. from functools import reduce and then call reduce() directly.

According to the documentation for reduce(), the function has the following signature:

functools.reduce(function, iterable[, initializer])

The Python documentation also states that reduce() is roughly equivalent to the following Python function:

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value

Like this Python function, reduce() works by applying a two-argument function to the items of iterable in a loop from left to right, ultimately reducing iterable to a single cumulative value.

Python’s reduce() also accepts a third and optional argument called initializer that provides a seed value to the computation or reduction.

In the next two sections, you’ll take an in-depth look at how Python’s reduce() works and the meaning behind each of its arguments.

The Required Arguments: function and iterable

The first argument to Python’s reduce() is a two-argument function conveniently called function. This function will be applied to the items in an iterable to cumulatively compute a final value.

Even though the official documentation refers to the first argument of reduce() as “a function of two arguments,” you can pass any Python callable to reduce() as long as the callable accepts two arguments. Callable objects include classes, instances that implement a special method called __call__(), instance methods, class methods, static methods, and functions.

The second required argument, iterable, will accept any Python iterable, as its name suggests. This includes lists, tuples, range objects, generators, iterators, sets, dictionary keys and values, and any other Python objects that you can iterate over.

To understand how reduce() works, you’re going to write a function that computes the sum of two numbers and prints the equivalent math operation to the screen. Here’s the code:

>>>
>>> def my_add(a, b):
...     result = a + b
...     print(f"{a} + {b} = {result}")
...     return result

This function calculates the sum of a and b, prints a message with the operation using an f-string, and returns the result of the computation. Here’s how it works:

>>>
>>> my_add(5, 5)
5 + 5 = 10
10

my_add() is a two-argument function, so you can pass it to Python’s reduce() along with an iterable to compute the cumulated sum of the items in the iterable. Check out the following code that uses a list of numbers:

>>>
>>> from functools import reduce

>>> numbers = [0, 1, 2, 3, 4]

>>> reduce(my_add, numbers)
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10

When you call reduce(), passing my_add() and numbers as arguments, you get an output that shows all the operations that reduce() performs to come up with a final result of 10. In this case, the operations are equivalent to ((((0 + 1) + 2) + 3) + 4) = 10.

The call to reduce() in the above example applies my_add() to the first two items in numbers (0 and 1) and gets 1 as the result. Then reduce() calls my_add() using 1 and the next item in numbers (which is 2) as arguments, getting 3 as the result. The process is repeated until numbers runs out of items and reduce() returns a final result of 10.

The Optional Argument: initializer

The third argument to Python’s