Loïc Le Loarer added the comment:
Thanks a lot for the clear answer, sorry for not having read the online
documentation, I only read the help(itertools.groupby) which has much less
details.
And for my use case, I can use an explicit command just to compute the number
of groups.
--
Eric V. Smith added the comment:
Loïc Le Loarer: Note that your use case isn't possible, anyway. There's no way
to know the number of groups until the input is exhausted, at which point
you've already iterated through all of the data.
--
nosy: +eric.smith
Raymond Hettinger added the comment:
Except for display the last few elements, this is the documented and intended
behavior: ( https://docs.python.org/3/library/itertools.html#itertools.groupby
):
'''
The returned group is itself an iterator that shares the underlying iterable
with groupby()
New submission from Loïc Le Loarer :
If I "list" the itertools groupby generator, then the sub generators of each
groups are all empty except the last one.
import itertools as i
L = ['azerty','abcd','ac','aaa','z','baba','bitte','rhum','z','y']
g = list(i.groupby(L, lambda x: x[0]))
number_of_g