classification
Title: Modification to "pairwise" in itertools recipes
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: LambertDW, della, georg.brandl, rhettinger
Priority: normal Keywords:

Created on 2009-02-23 13:37 by della, last changed 2009-02-23 21:57 by della. This issue is now closed.

Messages (7)
msg82626 - (view) Author: Matteo Dell'Amico (della) Date: 2009-02-23 13:37
I feel that the "pairwise" recipe could be slightly more elegant if "for
elem in b: break" became a simpler next(b) (or b.next() for Python 2.x).
It is also more natural to modify the recipes to suit one's needs (e.g.,
returning items with a given gap between them, or convert the recipe to
k-wise iteration).
msg82627 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-02-23 13:42
Assigning to Raymond.

Note that "for elem in b: break" and "next(b)" are not equivalent when b
is empty/exhausted.
msg82630 - (view) Author: Matteo Dell'Amico (della) Date: 2009-02-23 13:58
Georg, you're right, there's a StopIteration to catch. My thinko was
mistaking the function for a generator where the exception propagation
would have done the right thing. The amended version now becomes

next(b)
for x, y in zip(a, b): yield x, y

...which is not that attractive anymore, also because it's slower. Sorry
for the error.
msg82638 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-02-23 19:40
Applied in r69908 .
msg82640 - (view) Author: David W. Lambert (LambertDW) Date: 2009-02-23 20:13
Nice.  I had thought of this a while ago but found counter example,
probably using the empty iterator

def f():
    raise StopIteration
    yield


I didn't realize "next" had optional argument.
msg82641 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-02-23 20:19
Shame on me, I forgot about the optional argument too.
msg82646 - (view) Author: Matteo Dell'Amico (della) Date: 2009-02-23 21:57
great Raymond! :)
History
Date User Action Args
2009-02-23 21:57:27dellasetmessages: + msg82646
2009-02-23 20:19:21georg.brandlsetmessages: + msg82641
2009-02-23 20:13:42LambertDWsetmessages: + msg82640
2009-02-23 19:40:08rhettingersetstatus: open -> closed
resolution: accepted
messages: + msg82638
2009-02-23 15:20:31LambertDWsetnosy: + LambertDW
2009-02-23 13:58:29dellasetmessages: + msg82630
2009-02-23 13:42:13georg.brandlsetassignee: georg.brandl -> rhettinger
messages: + msg82627
nosy: + rhettinger
2009-02-23 13:37:11dellacreate