iterator question

George Sakkis george.sakkis at gmail.com
Tue Sep 26 19:45:15 CEST 2006


johnzenger at gmail.com wrote:

> def transform(seq, size):
>     i = 0
>     while i < len(seq):
>         yield tuple(seq[i:i+size])
>         i += size

Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
     it = iter(iterable)
     while True:
         window = tuple(islice(it,size))
         if not window: 
	     break
         yield window

George




More information about the Python-list mailing list