New submission from Med Nezar BELLAZRAK <bellazra...@gmail.com>:

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

----------
components: Extension Modules
messages: 318014
nosy: Med Nezar BELLAZRAK
priority: normal
severity: normal
status: open
title: itertools.groupby() returned igroup is only callable once
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33681>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to