Josh
Josh

Reputation: 12791

Unable to retrieve itertools iterators outside list comprehensions

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?

Upvotes: 2

Views: 64

Answers (1)

Mark
Mark

Reputation: 92440

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: 

Upvotes: 3

Related Questions