10000 Misc cleanups and wording improvements for the itertools docs by rhettinger · Pull Request #119626 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Misc cleanups and wording improvements for the itertools docs #119626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 27, 2024
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 wording for dropwhile()
  • Loading branch information
rhettinger committed May 27, 2024
commit 95e82ed29cee635fa02e9258a6c89ad52b0c3985
22 changes: 13 additions & 9 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ loops that truncate the stream.
# batched('ABCDEFG', 3) → ABC DEF G
if n < 1:
raise ValueError('n must be at least one')
iterable = iter(iterable)
while batch := tuple(islice(iterable, n)):
iterator = iter(iterable)
while batch := tuple(islice(iterator, n)):
if strict and len(batch) != n:
raise ValueError('batched(): incomplete batch')
yield batch
Expand Down Expand Up @@ -367,21 +367,25 @@ loops that truncate the stream.

.. function:: dropwhile(predicate, iterable)

Make an iterator that drops elements from the iterable as long as the predicate
is true; afterwards, returns every element. Note, the iterator does not produce
*any* output until the predicate first becomes false, so it may have a lengthy
start-up time. Roughly equivalent to::
Make an iterator that drops elements from the *iterable* while the
*predicate* is true and afterwards returns every element. Roughly
equivalent to::

def dropwhile(predicate, iterable):
# dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8
iterable = iter(iterable)
for x in iterable:

iterator = iter(iterable)
for x in iterator:
if not predicate(x):
yield x
break
for x in iterable:

for x in iterator:
yield x

Note this does not produce *any* output until the predicate first
becomes false, so this itertool may have a lengthy start-up time.


.. function:: filterfalse(predicate, iterable)

Expand Down
0