CKTIS / Python
itertools.dropwhile
Skips an initial run while the predicate holds, then yields the rest unchanged.
Version: Python 3.14; repository image, production runtime not attested
Syntax
itertools.dropwhile(predicate, iterable, /)Skips an initial run while the predicate holds, then yields the rest unchanged.
After the first failing item the predicate no longer filters later items, even when they satisfy it again.
The predicate discards the initial 1 and 2. It first fails at 4, which is yielded; from then on even the final 1 is yielded without testing. When bool succeeds for every input in the second call the whole prefix is dropped and no output remains.
import itertools as I
assert list(I.dropwhile(lambda n: n < 3, [1, 2, 4, 1])) == [4, 1]
assert list(I.dropwhile(bool, [1, 2])) == []
Examples
import itertools as I
print(list(I.dropwhile(lambda n: n < 3, [1, 2, 4, 1])))
print(list(I.dropwhile(bool, [1, 2])))
Result
[4, 1] []
The predicate discards the initial 1 and 2. It first fails at 4, which is yielded; from then on even the final 1 is yielded without testing. When bool succeeds for every input in the second call the whole prefix is dropped and no output remains.