Skip to content
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

gh-118476: update the example implementation for islice in docs #118466

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 13 additions & 17 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -504,24 +504,20 @@ loops that truncate the stream.
# islice('ABCDEFG', 2, None) → C D E F G
# islice('ABCDEFG', 0, None, 2) → A C E G
s = slice(*args)
start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
it = iter(range(start, stop, step))
try:
nexti = next(it)
except StopIteration:
# Consume *iterable* up to the *start* position.
for i, element in zip(range(start), iterable):
pass
start = s.start if s.start is not None else 0
stop = s.stop
step = s.step if s.step is not None else 1
it = iter(iterable)
for _ in zip(range(start), it):
# Consume up to *start* position
pass
if stop is not None and stop <= start:
return
try:
for i, element in enumerate(iterable):
if i == nexti:
yield element
nexti = next(it)
except StopIteration:
# Consume to *stop*.
for i, element in zip(range(i + 1, stop), iterable):
pass
for i, element in enumerate(it, start):
if (i - start) % step == 0:
Comment on lines +516 to +517
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding this comment here?

Suggested change
for i, element in enumerate(it, start):
if (i - start) % step == 0:
for i, element in enumerate(it, start):
# Consume to *stop*.
if (i - start) % step == 0:

yield element
if stop is not None and i + 1 >= stop:
return


.. function:: pairwise(iterable)
Expand Down