2

Consider this:

>>> res = [list(g) for k,g in itertools.groupby('abbba')]                                                                                                                                                           

>>> res                                                                                                                                                                                                             
[['a'], ['b', 'b', 'b'], ['a']]

and then this:

>>> res = [g for k,g in itertools.groupby('abbba')]

>>> list(res[0])
[]

I'm baffled by this. Why do they return different results?

Josh
  • 11,979
  • 17
  • 60
  • 96
  • Does this answer your question? [How to turn an itertools "grouper" object into a list](https://stackoverflow.com/questions/44490079/how-to-turn-an-itertools-grouper-object-into-a-list) – MrNobody33 Jul 18 '20 at 16:41

1 Answers1

3

This is expected behavior. The documentation is pretty clear that the iterator for the grouper is shared with the groupby iterator:

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

The reason you are getting empty lists as that the iterator is already consumed by the time you are trying to iterate over it.

import itertools 

res = [g for k,g in itertools.groupby('abbba')]

next(res[0])
# Raises StopIteration: 
Mark
  • 90,562
  • 7
  • 108
  • 148