]> granicus.if.org Git - python/commitdiff
Issue #10029: Fix sample code in the docs for zip().
authorRaymond Hettinger <python@rcn.com>
Sun, 10 Oct 2010 05:54:39 +0000 (05:54 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 10 Oct 2010 05:54:39 +0000 (05:54 +0000)
Doc/library/functions.rst

index 97f2a38d41aee3a295dfc8118c22fb2cd8658800..42e4269ace409ba7c439f9e9a26593d7a2f89fbe 100644 (file)
@@ -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