WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / Python

itertools.dropwhile

Überspringt die anfänglichen Elemente mit wahrem Prädikat.

Version: Python 3.14; repository image, production runtime not attested

Syntax

itertools.dropwhile(predicate, iterable, /)

Überspringt die anfänglichen Elemente mit wahrem Prädikat.

Der Test n < 3 verwirft 1 und 2 und endet bei 4. Er liefert 4 und die letzte 1 ohne weitere Prüfung.

Ist bool für alle Elemente wahr, bleibt die Ausgabe leer.

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])) == []

Beispiele

import itertools as I
print(list(I.dropwhile(lambda n: n < 3, [1, 2, 4, 1])))
print(list(I.dropwhile(bool, [1, 2])))

Ergebnis

[4, 1]
[]

Ist bool für alle Elemente wahr, bleibt die Ausgabe leer.

Quellen