summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-10-10 05:54:39 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-10-10 05:54:39 (GMT)
commit2f08df36900f4a2799e7f0e92178e314180ee3bc (patch)
tree5db7c3398b931df194a9ea017382c3eca1a56bc6
parent5b0e9e84e9833337f791d5bc74f823a16681924b (diff)
downloadcpython-2f08df36900f4a2799e7f0e92178e314180ee3bc.zip
cpython-2f08df36900f4a2799e7f0e92178e314180ee3bc.tar.gz
cpython-2f08df36900f4a2799e7f0e92178e314180ee3bc.tar.bz2
Issue #10029: Fix sample code in the docs for zip().
-rw-r--r--Doc/library/functions.rst17
1 files changed, 12 insertions, 5 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 97f2a38..42e4269 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -1235,11 +1235,18 @@ are always available. They are listed here in alphabetical order.
iterable argument, it returns an iterator of 1-tuples. With no arguments,
it returns an empty iterator. Equivalent to::
- def zip(*iterables):
- # zip('ABCD', 'xy') --> Ax By
- iterables = map(iter, iterables)
- while iterables:
- yield tuple(map(next, iterables))
+ def zip(*iterables):
+ # zip('ABCD', 'xy') --> Ax By
+ sentinel = object()
+ iterables = [iter(it) for it in iterables]
+ while iterables:
+ result = []
+ for it in iterables:
+ elem = next(it, sentinel)
+ if elem is sentinel:
+ return
+ result.append(elem)
+ yield tuple(result)
The left-to-right evaluation order of the iterables is guaranteed. This
makes possible an idiom for clustering a data series into n-length groups