Serhiy Storchaka added the comment: Does it make sense using a grouper after advancing the groupby iterator? Currently it is possible by accident:
>>> from itertools import groupby, count >>> it = groupby(count(), lambda i: (i//10)%2) >>> _, even = next(it) >>> _, odd = next(it) >>> print(list(even)) [] >>> print(list(odd)) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> print(list(even)) [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] >>> print(list(odd)) [30, 31, 32, 33, 34, 35, 36, 37, 38, 39] >>> print(list(even)) [40, 41, 42, 43, 44, 45, 46, 47, 48, 49] But I think that it would be more consistent to implement one of following variant: 1. Invalidate groupers after creating a new grouper. There should be only one valid grouper related to the groupby object. Using of invalid grouper should raise either StopIteration or RuntimeError. >>> it = groupby(count(), lambda i: (i//10)%2) >>> _, even = next(it) >>> _, odd = next(it) >>> print(list(even)) [] >>> print(list(odd)) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> print(list(even)) [] >>> print(list(odd)) [] or >>> it = groupby(count(), lambda i: (i//10)%2) >>> _, even = next(it) >>> _, odd = next(it) >>> print(list(even)) Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError >>> print(list(odd)) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> print(list(even)) Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError >>> print(list(odd)) [] 2. Duplicate the source iterator using tee() when create a new grouper. All groupers can be used independently from the groupby object and other groupers. >>> it = groupby(range(100), lambda i: (i//10)%2) >>> _, even = next(it) >>> _, odd = next(it) >>> _ = list(it) # exhaust the source iterator >>> print(list(even)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print(list(odd)) [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> print(list(even)) [] >>> print(list(odd)) [] I think resolving this issue will also fix issue30347. ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30346> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com