Slicing a list into a list of sub-lists [duplicate]

[input[i:i+n] for i in range(0, len(input), n)]        # Use xrange in py2k

where n is the length of a chunk.

Since you don’t define what might happen to the final element of the new list when the number of elements in input is not divisible by n, I assumed that it’s of no importance: with this you’ll get last element equal 2 if n equal 7, for example.

Leave a Comment