A problem with itertools.groupby

Antoon Pardon antoon.pardon at vub.be
Fri Dec 17 10:41:57 EST 2021


> 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.

The group-iterators are connected. Each group-iterator is a wrapper 
around the original iterator
with an extra termination condition. So in order to start the next 
group-iterator the previous
group-iterator is exhausted, because the original iterator has to be 
ready to produce values
for the next group-iterator.

-- 
Antoon Pardon.


More information about the Python-list mailing list