How to Compress a Generator with a Boolean Mask in Python

Filters items from a generator based on a parallel boolean mask, yielding only the items where the mask is True.

Easy Python 3.9+ Aug 9, 2026 Comprehensions & generators 14 views 0 copies

Python code

11 lines
Python 3.9+
def compress(generator, mask):
    for item, keep in zip(generator, mask):
        if keep:
            yield item


if __name__ == "__main__":
    data = [1, 2, 3, 4, 5]
    mask = [True, False, True, False, True]
    result = list(compress(iter(data), mask))
    print(result)

Output

stdout
[1, 3, 5]

How it works

The zip function pairs each item from the generator with the corresponding boolean from the mask, stopping when either is exhausted. The if keep condition only yields items where the mask value is truthy. This lazy generator avoids building an intermediate list of all items, making it memory-efficient for large or infinite sequences. Using iter(data) is redundant here since lists are already iterable, but it clarifies that the function accepts any iterable.

Common mistakes

  • Forgetting to convert the generator result to a list before printing it.
  • Assuming the mask and generator must be the same length; zip stops at the shorter one.
  • Using a truthy check on non-boolean values, which may filter unintentionally.
  • Not realizing that the function returns a generator, so it must be consumed.

Variations

  1. Use `itertools.compress(data, mask)` from the standard library for a built-in equivalent.
  2. Use a list comprehension: `[item for item, keep in zip(data, mask) if keep]`.

Real-world use cases

  • Filtering sensor readings where a companion boolean array indicates valid measurements.
  • Selecting certain log entries based on a pre-computed mask of relevance flags.
  • Applying a subset of items from a large stream of API responses matching a user's filter criteria.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Comprehensions & generators

Related tutorials and quizzes for this topic.