How to split a list into equally-sized chunks with examples
Sometimes, list of elements needs to convert a sublist of equal sized chunks.
For example, List contains below elements
number = [11,12,13,14,5,9,11,20]
Output:
[(11, 12, 13), (14, 5, 9), (11, 20)]
This tutorial explains how to convert a list into split of list’s list.
Python List chunk into equal sizes
Multiple approach to convert list into equal list of lists.
from itertools import islice
numbers = [11,12,13,14,5,9,11,20]
def chunkslist(numbers, size):
numbers = iter(numbers)
return iter(lambda: tuple(islice(numbers, size)), ())
print(list(chunkslist(numbers,3)))