Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 066e7a9

Browse files
committed
Issue #10029: Fix sample code in the docs for zip().
1 parent ae136da commit 066e7a9

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

Doc/library/functions.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,11 +1220,18 @@ are always available. They are listed here in alphabetical order.
12201220
iterable argument, it returns an iterator of 1-tuples. With no arguments,
12211221
it returns an empty iterator. Equivalent to::
12221222

1223-
def zip(*iterables):
1224-
# zip('ABCD', 'xy') --> Ax By
1225-
iterables = map(iter, iterables)
1226-
while iterables:
1227-
yield tuple(map(next, iterables))
1223+
def zip(*iterables):
1224+
# zip('ABCD', 'xy') --> Ax By
1225+
sentinel = object()
1226+
iterables = [iter(it) for it in iterables]
1227+
while iterables:
1228+
result = []
1229+
for it in iterables:
1230+
elem = next(it, sentinel)
1231+
if elem is sentinel:
1232+
return
1233+
result.append(elem)
1234+
yield tuple(result)
12281235

12291236
The left-to-right evaluation order of the iterables is guaranteed. This
12301237
makes possible an idiom for clustering a data series into n-length groups

0 commit comments

Comments
 (0)