From: Raymond Hettinger Date: Sun, 10 Oct 2010 05:56:57 +0000 (+0000) Subject: Issue #10029: Fix sample code in the docs for zip(). X-Git-Tag: v3.1.3rc1~134 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=066e7a974afa8582fde9e1f97d76f76ee9ffc216;p=thirdparty%2FPython%2Fcpython.git Issue #10029: Fix sample code in the docs for zip(). --- diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index efcac085af3c..e97be7b9d03e 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1220,11 +1220,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