Reading 3 objects at a time from list

Vito De Tullio zak.mc.kraken at libero.it
Sat Apr 11 21:48:09 CEST 2009


Matteo wrote:

> it works and I like slices, but I was wondering if there was another
> way of doing the same thing, maybe reading the numbers in groups of
> arbitrary length n...

from http://docs.python.org/library/itertools.html#recipes

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)


and it's *damned* fast!


-- 
By ZeD




More information about the Python-list mailing list