summaryrefslogtreecommitdiffstats
path: root/Doc/library/itertools.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2017-09-07 21:01:44 (GMT)
committerGitHub <noreply@github.com>2017-09-07 21:01:44 (GMT)
commit3147b0422cbeb98065666ccf95ab6845ac800fd4 (patch)
treeaef004c01f17bd07f41828558b535bae4828724d /Doc/library/itertools.rst
parent0c72a0c449e69949a236452579a6820e6eaa4d87 (diff)
downloadcpython-3147b0422cbeb98065666ccf95ab6845ac800fd4.zip
cpython-3147b0422cbeb98065666ccf95ab6845ac800fd4.tar.gz
cpython-3147b0422cbeb98065666ccf95ab6845ac800fd4.tar.bz2
bpo-31270: Modification of Pr 3200 (#3427)
* bpo-31270: Simplify documentation of itertools.zip_longest * Use repeat(). Track num_active.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r--Doc/library/itertools.rst37
1 files changed, 18 insertions, 19 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index b0d0a8c..c989e46 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -630,26 +630,25 @@ loops that truncate the stream.
iterables are of uneven length, missing values are filled-in with *fillvalue*.
Iteration continues until the longest iterable is exhausted. Roughly equivalent to::
- class ZipExhausted(Exception):
- pass
-
- def zip_longest(*args, **kwds):
+ def zip_longest(*args, fillvalue=None):
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
- fillvalue = kwds.get('fillvalue')
- counter = len(args) - 1
- def sentinel():
- nonlocal counter
- if not counter:
- raise ZipExhausted
- counter -= 1
- yield fillvalue
- fillers = repeat(fillvalue)
- iterators = [chain(it, sentinel(), fillers) for it in args]
- try:
- while iterators:
- yield tuple(map(next, iterators))
- except ZipExhausted:
- pass
+ iterators = [iter(it) for it in args]
+ num_active = len(iterators)
+ if not num_active:
+ return
+ while True:
+ values = []
+ for i, it in enumerate(iterators):
+ try:
+ value = next(it)
+ except StopIteration:
+ num_active -= 1
+ if not num_active:
+ return
+ iterators[i] = repeat(fillvalue)
+ value = fillvalue
+ values.append(value)
+ yield tuple(values)
If one of the iterables is potentially infinite, then the :func:`zip_longest`
function should be wrapped with something that limits the number of calls