Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve clarity of takewhile() example using unique inputs
  • Loading branch information
rhettinger committed May 23, 2024
commit 3b8195b6d4b9ad0f7f0b3eb36f0573aaa712e294
7 changes: 4 additions & 3 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,15 @@ loops that truncate the stream.
the output tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their
value. So if the input elements are unique, there will be no repeated
value. So, if the input elements are unique, there will be no repeated
values within a permutation.

Roughly equivalent to::

def permutations(iterable, r=None):
# permutations('ABCD', 2) → AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) → 012 021 102 120 201 210

pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
Expand All @@ -575,7 +576,7 @@ loops that truncate the stream.
return

The code for :func:`permutations` can be also expressed as a subsequence of
:func:`product`, filtered to exclude entries with repeated elements (those
:func:`product` filtered to exclude entries with repeated elements (those
from the same position in the input pool)::

def permutations(iterable, r=None):
Expand Down Expand Up @@ -669,7 +670,7 @@ loops that truncate the stream.
predicate is true. Roughly equivalent to::

def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) → 1 4
# takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4
for x in iterable:
if not predicate(x):
break
Expand Down