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.

Author Med Nezar BELLAZRAK
Recipients Med Nezar BELLAZRAK
Date 2018-05-29.12:02:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1527595366.08.0.682650639539.issue33681@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2018-05-29 12:02:46Med Nezar BELLAZRAKsetrecipients: + Med Nezar BELLAZRAK
2018-05-29 12:02:46Med Nezar BELLAZRAKsetmessageid: <1527595366.08.0.682650639539.issue33681@psf.upfronthosting.co.za>
2018-05-29 12:02:46Med Nezar BELLAZRAKlinkissue33681 messages
2018-05-29 12:02:46Med Nezar BELLAZRAKcreate