Skip to content
Python compress: validate selector counts before filtering

Python compress: validate selector counts before filtering

November 20, 2026

itertools.compress stops when either input ends. A result that looks plausible can hide a missing selector, and extra false selectors leave no visible trace in the filtered output.

Set up

Tested on Linux with CPython 3.12.14. Use Python 3.12 or later, Bash, a text editor, mktemp, rm and rmdir. The example needs no network access. Run the steps in the same terminal and stop if setup fails. Run Python without -O so the assertions stay enabled.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as validate_selectors.py:

from itertools import compress

rows = ["alpha", "beta", "gamma"]
print("short selectors:", list(compress(rows, [True, False])))
print("extra false selector:", list(compress(rows, [True, False, True, False])))

def select_checked(data, selectors):
    data = list(data)
    selectors = list(selectors)
    if len(data) != len(selectors):
        raise ValueError(f"{len(data)} rows but {len(selectors)} selectors")
    return list(compress(data, selectors))

published = []
for selectors in ([True, False], [True, False, True, False], [True, False, True]):
    try:
        selected = select_checked(rows, selectors)
    except ValueError as exc:
        print("rejected:", exc)
    else:
        published.extend(selected)
print("published:", published)
assert published == ["alpha", "gamma"]

Run:

python3 -B validate_selectors.py

Observed output:

short selectors: ['alpha']
extra false selector: ['alpha', 'gamma']
rejected: 3 rows but 2 selectors
rejected: 3 rows but 4 selectors
published: ['alpha', 'gamma']

Validate the unfiltered lengths

The compress documentation defines selection by truth values and termination when either iterable is exhausted. The fourth false selector still counts as an input item, even though it could never add a row to the output. Counting selected rows therefore cannot validate the original pairing.

The helper snapshots both finite inputs, compares their full lengths, and only then filters. It also materializes the filtered result before the caller changes published. The two mismatches are rejected without publishing any selected rows; the valid selector list contributes two rows.

Limits

This uses memory proportional to both inputs and the result, so it is intended for bounded collections. Reading either iterator can itself have effects or raise an exception; this helper cannot undo those effects. Selectors use truthiness rather than requiring the bool type. If selectors come from text, parse them explicitly: the nonempty string "false" is truthy. Custom truth-value methods can also fail during filtering.

Clean up

Remove the example file and the temporary directory:

rm -- validate_selectors.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Acknowledgements

This post was created with assistance from AI.

Last updated on