Best pattern/idiom
Chris Connett
chrisccc_3k at yahoo.com
Mon Aug 9 14:35:01 EDT 2004
I was wondering if there were a well known pattern/idiom for breaking a
sequence into a list of lists, each n elements long, e.g.
[0,1,2,3,4,5,6,7,8,9,10,11] -> [[0,1,2,3],[4,5,6,7],[8,9,10,11]]
This comes up reasonably often in my work, and every time I re-think
about it, and come up with
[ lis[n:n+4] for n in range( 0, len( lis ), 4 ) ]
which seems very kludgy to me, since it uses a range and len, 2 mentions
of the list identifier and 2 literal 4's (which is the size I want to
break into this time).
Is there a better way?
More information about the Python-list
mailing list