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
Next Next commit
Move try/except out of the inner loop.
  • Loading branch information
rhettinger committed May 17, 2024
commit 6f7d1e9425251563c4c68578c04fa0c7a83af3bc
18 changes: 9 additions & 9 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -698,18 +698,18 @@ loops that truncate the stream.

def tee(iterable, n=2):
iterator = iter(iterable)
empty_link = [None, None] # Singly linked list: [value, link]
return tuple(_tee(iterator, empty_link) for _ in range(n))
initial_link = [None, None] # node: [data, link]
return tuple(_tee(iterator, initial_link) for _ in range(n))

def _tee(iterator, link):
while True:
if link[1] is None:
try:
try:
while True:
if link[1] is None:
link[:] = [next(iterator), [None, None]]
except StopIteration:
return
value, link = link
yield value
value, link = link
yield value
except StopIteration:
return

Once a :func:`tee` has been created, the original *iterable* should not be
used anywhere else; otherwise, the *iterable* could get advanced without
Expand Down