[New-bugs-announce] [issue30346] Odd behavior when unpacking `itertools.groupby`

Matt Gilson report at bugs.python.org
Thu May 11 13:33:01 EDT 2017


New submission from Matt Gilson:

There is some odd behavior when unpacking the groups from an itertools.groupby result.  For example:

    from itertools import groupby
    from operator import itemgetter

    inputs = ((x > 5, x) for x in range(10))
    (_, a), (_, b) = groupby(inputs, key=itemgetter(0))
    print(list(a))
    print(list(b))

On CPython, this results in:

    []
    [(True, 9)]

I would expect it to print out 2 empty lists since the second group would have to be consumed to make sure that there isn't another value yielded from groupby (If there was another value to yield, we'd get a ValueError since we couldn't unpack the correct number of items).

This is the behavior that PyPy had prior to re-implementing to be consistent with CPython in https://bitbucket.org/pypy/pypy/commits/6093ff1a44e6b17f09db83aa80aea562a738c286

----------
messages: 293511
nosy: Matt Gilson
priority: normal
severity: normal
status: open
title: Odd behavior when unpacking `itertools.groupby`
type: behavior
versions: Python 3.3, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30346>
_______________________________________


More information about the New-bugs-announce mailing list