Python Glossary Русский

all Python 3itertoolsfunction

itertools.dropwhile

itertools

Drop elements while the predicate is true, then yield the rest.

Syntax

itertools.dropwhile(predicate, iterable)

The predicate is tested only until it first returns False — after that dropwhile passes the entire remainder through unchanged, even elements that would again satisfy it. That is exactly what separates it from filter, which evaluates every element individually.

Examples

import itertools
print(list(itertools.dropwhile(lambda x: x < 3, [1, 2, 3, 1, 2])))   # → [3, 1, 2]
print(list(itertools.dropwhile(lambda x: x == 0, [0, 0, 5, 0, 7])))  # → [5, 0, 7]
print(''.join(itertools.dropwhile(str.isspace, '   код')))  # → код
print(list(itertools.dropwhile(lambda x: x > 0, [1, 2, 3])))  # → []
print(list(filter(lambda x: x >= 3, [1, 2, 3, 1, 2])))  # → [3]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy