ID EN
Itertools

compress

Python 3.11

Make an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to True. Stops when either the data or selectors iterables has been exhausted. Roughly equivalent to:

Syntax

PYTHON
itertools.compress(data, selectors)

Examples

Example 1
PYTHON
def compress(data, selectors):
     compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
    return (d for d, s in zip(data, selectors) if s)