On this page
itertools.dropwhile
Prev: itertools.cycle | Up: itertools | Next: itertools.filterfalse
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.dropwhile
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import dropwhile
Signature & Overview
itertools.dropwhile(predicate, iterable, /)
Drop items from the iterable while predicate(item) is true.
Usage
from itertools import dropwhile
# Drops leading elements < 5, then yields EVERYTHING afterward
assert list(dropwhile(lambda x: x < 5, [1, 4, 6, 2, 8])) == [6, 2, 8]
Gotchas
- Stops filtering after the first falsy element:
2is kept in[1, 4, 6, 2, 8]becausedropwhileonly skips a prefix. Usefilter()orfilterfalse()to filter the whole sequence. - No output until the predicate fails: If every element satisfies the predicate, the entire iterable is consumed and nothing is yielded.
REPL Inspect
from itertools import dropwhile
help(dropwhile)
Linked from
Related notes
- tuts/python3/stdlib_module/itertools/cycle Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/filterfalse Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/accumulate Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/compress shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/count shared tags: #itertools, #stdlib