Python Supply
Published in

Python Supply

Comprehensions and Combinations

If you are working on a project in which you must enumerate, traverse, and/or test every possible combination of elements from one or more finite (or even infinite) collections, Python provides a variety of ways to do so. This article reviews some of the syntactically concise ways to do so, while also addressing some relevant memory utilization aspects. In particular, the focus is on comprehension syntax as foundational building block that can be employed in conjunction with functions and recursion.

Conventions for Terminology and Notation

In order to maintain consistency across examples and to keep outputs deterministic, this articles follows the conventions enumerated below.

  • Python lists are used to represent both collections that may have duplicates and sets that do not have duplicates. For example, the set {0, 1, 2} is represented using [0, 1, 2].
  • When including multiple collections (e.g., U, V, and W) within a Cartesian product expression (e.g., U × V × W), the collections are called components of the Cartesian product.
  • When an output evaluates to an iterable, it is sometimes immediately consumed and turned into a list using the list function so that it can be reused throughout an example multiple times. In practice, this may not be necessary and may even be unnecessarily expensive in terms of both memory utilization and running time. This distinction is noted explicitly where applicable.
  • A single-letter variable x usually refers to individual elements in a collection, a variable such as xs usually refers to collections of elements, and a variables such as xss usually refers to a collection of collections.

Cartesian Products

One of the simplest scenarios is one in which it is necessary to generate every combination of elements from two or more collections, where each combination has one component from each collection. This corresponds to the Cartesian product of the collections. Python’s comprehension syntax is a powerful language feature that, under the right circumstances, provides a way to implement such operations involving collections in a way that is concise and closely resembles widely used and recognized forms of mathematical notation. The example below demonstrates how this syntax can be used to build a Cartesian product of two lists.

Comprehension syntax can become difficult to manage and read as the number of component collections involved increases. In such cases, it may make sense to build a function so that the repetitive aspects of building the definition are handled programmatically. It is demonstrated at the end of this section how such a function can be defined. But first, some examples of how it can be used are illustrated using the product function found in the built-in itertools library.

The product function takes any number of iterable arguments and creates an iterable result containing one tuple for every combination of elements from each of the arguments.

Using the unpacking operator in conjunction with the list repetition operator (also available as a method), it is possible to concisely describe a Cartesian product of any number of instances of a finite collection. In the example below, a ten-dimensional discrete finite space is created (where each dimension is [0, 1, 2]).

How much memory does an output of product consume? Because it is an iterable that generates data dynamically, it only takes about as much memory as the component collections provided as inputs to product.

Now that it has been demonstrated how the function can be used, consider a recursive implementation of such a function. To understand how this can be accomplished, a concrete example may help. Suppose a Cartesian product p of two collections [False, True] and [0, 1] has already been built. How do you turn it into a Cartesian product of three collections? You can iterate over all combinations of elements from the third collection ["a", "b"] and from the Cartesian product p, concatenating each element-combination pair.

Below is a complete implementation of the function based on the above approach. Note that the base case is a collection containing a single tuple of length zero. The recursive case consists of a comprehension that prepends every element in the first collection to every tuple in the cartesian product of all the remaining collections.

You can confirm that cart produces the same output as product. However, note that its result is generated in its entirety when the function is called.

A variant of this function that uses memory more efficiently can be created by turning the original definition into a generator. One added benefit of this approach is that the component collections can now be generators themselves (and, thus, can potentially contain an unknown number or even infinitely many elements).

The generator variant of cart function is nearly identical to product; the only difference is the absence of argument unpacking.

Because this function is a generator, it consumes approximately as much memory as is needed to keep track of the two component collections.

Power Sets

A closely related scenario is one in which it may be necessary to generate every subset of a finite set (also known as the power set). There are a number of approaches to building a power set. It is possible to use the Cartesian product as a building block by noting that every element in a set s of size len(s) is either absent (corresponding to False) or present (corresponding to True). Thus, you can first build a Cartesian product of len(s) instances of the set {False, True}.

It is then possible to use the built-in zip function and a comprehension (which employs an if clause for filtering) to associate the boolean values that make up each of the tuples in the Cartesian product with the corresponding elements in the original set.

Alternatively, the Python documentation provides a recipe for building power sets using the built-in itertools library.

The above variant yields a collection of tuples rather than sets, but otherwise produces the same collection of combinations.

The recursive definition below employs an approach that is nearly identical to that of the recursive definition of the Cartesian production function. In the recursive case, every subset of all the remaining elements (excluding the current first element) is included in the overall result. However, a second copy of each of these subsets is taken and then paired (as in the Cartesian product function) with the current first element. This accounts for all subsets that do include the first element and all subsets that do not include it.

This approach yields the same results as the definition in the recipe, though it is worth noting that the exact implementation above leads to a different order. This distinction may be important in some applications (such as when performing a search for the largest subset that satisfies some criteria).

Can you apply the same technique used in the implementations of the cart function to turn the above recursive definition of powerset into a generator?

Further Reading

This article reviews how comprehensions, in concert with other built-in functions and Python features, can be used to assemble concise implementations of algorithms for generating common kinds of combinations of items from a collection. Many variants and relevant peripheral techniques can be found in a list of recipes found in the documentation for the built-in itertools library. This includes functions such as combinations, which can be used to obtain all subsets of a specific size. Other common set operations such as union and intersection are available as methods of the built-in set type, and the built-in collections library provides a variety of operations on collections (such as a class Counter for concisely implementing counting workflows). Popular third-party libraries such as SciPy contain specialized functions for common operations in combinatorics.

This article is also available as a Jupyter Notebook and in HTML form on GitHub.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store