On this page
  1. itertools.takewhile
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

itertools.takewhile

Prev: itertools.starmap | Up: itertools | Next: itertools.tee
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.takewhile
Tags: #public #python3 #stdlib #itertools


Import

import itertools
from itertools import takewhile

Signature & Overview

itertools.takewhile(predicate, iterable, /)

Return successive entries from an iterable as long as the predicate evaluates to true for each entry.

Usage

from itertools import takewhile

# Yields leading elements < 5 and stops immediately at 6
assert list(takewhile(lambda x: x < 5, [1, 4, 6, 2, 8])) == [1, 4]

Gotchas

REPL Inspect

from itertools import takewhile
help(takewhile)

Linked from

Related notes