Reading 3 objects at a time from list

Vito De Tullio at
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...


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