How to chop list n-elements at a time

cpsoct at my-deja.com cpsoct at my-deja.com
Sat Jan 20 03:56:40 EST 2001


I've been at this a while and can't seem to get it to work. I am
wondering if others know how, have tried, or already have written a
func to package up list elements in sublists n at a time.

I was able to get something that worked for bunches of 1, or 2, but i
am wondering how to make the tool more general so that i could say:

x=[1,2,3,4,5,6,7,8,9]

bunch(x,1) --> [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
bunch(x, 2) --> [[1,2], [3,4], [5,6], [7,8], [9, None]
bunch(x, 3) --> [[1,2,3], [4,5,6], [7,8,9]]
bunch(x, 4) --> [1,2,3,4], [5,6,7,8] [9, None, None, None]]


The algo. i was using for two at a time for example just tested for
even and odd. but i am not sure how to do generalize to create the
above.

cheers,
kevin parks
cpsoct at lycos.com





Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list