Simple List division problem

Paul Rubin http
Sun Jan 13 14:28:00 CET 2008


thebjorn <BjornSteinarFjeldPettersen at gmail.com> writes:
> Perhaps something like this?
> 
> def chop(lst, length):
>     from itertools import islice
>     it = iter(lst)
>     z = [list(islice(it, length)) for i in xrange(1 + len(lst) // length)]
>     if len(z) > 1:
>         z[-2].extend(z.pop()) # the last item will be empty or contain "overflow" elements.
>     return z

def chop(lst, length):
    def chop1():
       t = len(lst) // length - 1
       for i in xrange(t):
          yield lst[i*length: (i+1)*length]
       yield lst[t*length:]
    return list(chop1())



More information about the Python-list mailing list