A problem with itertools.groupby

Peter Pearson pkpearson at nowhere.invalid
Fri Dec 17 11:06:10 EST 2021


On Fri, 17 Dec 2021 09:25:03 +0100, ast <ast at invalid> wrote:
[snip]
>
> but:
>
> li = [grp for k, grp in groupby("aahfffddssssnnb")]
> list(li[0])
>
> []
>
> list(li[1])
>
> []
>
> It seems empty ... I don't understand why, this is
> the first read of an iterator, it should provide its
> data.

Baffling.  Here's a shorter and less readable illustration:


>>> list(groupby("aabbb"))
[('a', <itertools._grouper object at 0xb7018c0c>), 
 ('b', <itertools._grouper object at 0xb7018a0c>)]
>>> list(groupby("aabbb"))[0]
('a', <itertools._grouper object at 0xb71d3e4c>)
>>> list(list(groupby("aabbb"))[0][1])
[]


-- 
To email me, substitute nowhere->runbox, invalid->com.


More information about the Python-list mailing list