Skip to content

Commit 81c3130

Browse files
authored
Minor improvements to the docs for itertools.tee() (gh-119135)
1 parent 31a28cb commit 81c3130

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Doc/library/itertools.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -698,18 +698,19 @@ loops that truncate the stream.
698698

699699
def tee(iterable, n=2):
700700
iterator = iter(iterable)
701-
empty_link = [None, None] # Singly linked list: [value, link]
702-
return tuple(_tee(iterator, empty_link) for _ in range(n))
701+
shared_link = [None, None]
702+
return tuple(_tee(iterator, shared_link) for _ in range(n))
703703

704704
def _tee(iterator, link):
705-
while True:
706-
if link[1] is None:
707-
try:
708-
link[:] = [next(iterator), [None, None]]
709-
except StopIteration:
710-
return
711-
value, link = link
712-
yield value
705+
try:
706+
while True:
707+
if link[1] is None:
708+
link[0] = next(iterator)
709+
link[1] = [None, None]
710+
value, link = link
711+
yield value
712+
except StopIteration:
713+
return
713714

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

0 commit comments

Comments
 (0)