Iteration Classes

itertools.compress()

Keep values whose corresponding selector is truthy. Stop when either input is exhausted.

Formal Syntax & Structure

python syntaxpython
itertools.compress(data, selectors)

Parameters & Attributes

NameTypeDescription
dataiterableValues to select from.
selectorsiterableTruth-tested selectors corresponding to data.

Return Value

iterator

The iterator result described above.

Working Example

Example Snippetpython
import itertoolsprint(list(itertools.compress("ABCD",[1,0,1,0])))
Python Reference