generate tuples from sequence

Will McGugan will at willmcgugan.com
Wed Jan 17 07:41:02 EST 2007


Hi,

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)

This is what I came up with..

    def take_group(gen, count):
        i=iter(gen)
        while True:
            yield tuple([i.next() for _ in xrange(count)])

Is this the most efficient solution?

Regards,

Will McGugan
--
http://www.willmcgugan.com




More information about the Python-list mailing list