Slicing iterables in sub-generators without loosing elements

Ian Kelly ian.g.kelly at gmail.com
Sat Sep 29 12:36:39 EDT 2012


On Sat, Sep 29, 2012 at 10:14 AM, Thomas Bach
<thbach at students.uni-mainz.de> wrote:
> Hi,
>
> say we have the following:
>
>>>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)]
>
> is there a way to code a function iter_in_blocks such that
>
>>>> result = [ list(block) for block in iter_in_blocks(data) ]
>
> evaluates to
>
>>>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ]
>
> by _only_ _iterating_ over the list (caching all the elements sharing
> the same first element doesn't count)?

Am I correct in understanding that the intent is that the "blocks" are
groups that share the same first item?

Is it guaranteed that the blocks will be contiguous?  If so, you could
use itertools.groupby:

from itertools import groupby, imap
from operator import itemgetter

def iter_in_blocks(data):
    return imap(itemgetter(1), groupby(data, itemgetter(0)))

If there is no such guarantee, then the list would need to be sorted first.



More information about the Python-list mailing list