Slicing iterables in sub-generators without loosing elements

Paul Rubin no.email at nospam.invalid
Sat Sep 29 12:26:00 EDT 2012


Thomas Bach <thbach at students.uni-mainz.de> writes:
>>>> 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)?

itertools.groupby(data, lambda (x,y) : x)

is basically what you want.



More information about the Python-list mailing list