This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: itertools.groupby() returned igroup is only callable once
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Med Nezar BELLAZRAK, steven.daprano
Priority: normal Keywords:

Created on 2018-05-29 12:02 by Med Nezar BELLAZRAK, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg318014 - (view) Author: Med Nezar BELLAZRAK (Med Nezar BELLAZRAK) Date: 2018-05-29 12:02
Hello,

So i discovered a small unusual behavior (tracking it down was time-consuming) when using itertools.groupby(), i have checked the documentation and it states that:

"The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list"

I do agree with this statement, though i believe a call to igroup successively in the same iteration should yield the same result while now it returns the correct igroup in the first call while the following call returns an empty list.

Here's a small code snippet that illustrates this behavior:

Code:

import itertools

mylist = [(1,2), (1,3), (2,5)]

for key, igroup in itertools.groupby(mylist, lambda x: x[0]):
    print(list(igroup)) # prints the expected igroup
    print(list(igroup)) # prints an empty list
    print(list(igroup)) # prints an empty list

Output:

[(1, 2), (1, 3)]
[]
[]
[(2, 5)]
[]
[]

Thanks in advance for anyone who works on this issue
msg318015 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-05-29 12:12
This is standard behaviour for all iterators. Once you have iterated over them once, they are consumed, and iterating over them again returns nothing:

py> it = iter("abc")
py> print(list(it))
['a', 'b', 'c']
py> print(list(it))
[]
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77862
2018-05-29 12:12:54steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg318015

resolution: not a bug
stage: resolved
2018-05-29 12:02:46Med Nezar BELLAZRAKcreate