list()
and range()
to create a list of the desired size
.map()
on the list and fill it with splices of the given list.from math import ceil def chunk(lst, size): return list( map(lambda x: lst[x * size:x * size + size], list(range(ceil(len(lst) / size)))))
chunk([1, 2, 3, 4, 5], 2) # [[1, 2], [3, 4], [5]]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️