generate tuples from sequence

Peter Otten __peter__ at web.de
Wed Jan 17 14:46:03 CET 2007


Will McGugan wrote:

> I'd like a generator that takes a sequence and yields tuples containing
> n items of the sqeuence, but ignoring the 'odd' items. For example
> 
> take_group(range(9), 3) -> (0,1,2) (3,4,5) (6,7,8)

I like

>>> items = range(9)
>>> N = 3
>>> zip(*[iter(items)]*N)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]

Peter



More information about the Python-list mailing list