summaryrefslogtreecommitdiffstats
path: root/Doc/library/itertools.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-10-30 22:07:01 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-10-30 22:07:01 (GMT)
commit5d12faa5b873bc85778b5a68b340eed659fa3519 (patch)
treefe114a53edcbb15ce5691648ca77243b97312a04 /Doc/library/itertools.rst
parent385c803658e7603f0b416de0d06a9fc967e58051 (diff)
parent6f45d18c2474097a36bec5a769f30d33cd1b12be (diff)
downloadcpython-5d12faa5b873bc85778b5a68b340eed659fa3519.zip
cpython-5d12faa5b873bc85778b5a68b340eed659fa3519.tar.gz
cpython-5d12faa5b873bc85778b5a68b340eed659fa3519.tar.bz2
Merge
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r--Doc/library/itertools.rst23
1 files changed, 16 insertions, 7 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 9cdad6e..7b5922a 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -595,16 +595,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. Equivalent to::
- def zip_longest(*args, fillvalue=None):
+ class ZipExhausted(Exception):
+ pass
+
+ def zip_longest(*args, **kwds):
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
- def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
- yield counter() # yields the fillvalue, or raises IndexError
+ fillvalue = kwds.get('fillvalue')
+ counter = len(args) - 1
+ def sentinel():
+ nonlocal counter
+ if not counter:
+ raise ZipExhausted
+ counter -= 1
+ yield fillvalue
fillers = repeat(fillvalue)
- iters = [chain(it, sentinel(), fillers) for it in args]
+ iterators = [chain(it, sentinel(), fillers) for it in args]
try:
- for tup in zip(*iters):
- yield tup
- except IndexError:
+ while iterators:
+ yield tuple(map(next, iterators))
+ except ZipExhausted:
pass
If one of the iterables is potentially infinite, then the :func:`zip_longest`