Files
30-seconds-of-code/snippets/chunk.md
Rohit Tanwar a09142e1eb add chunk
2018-01-09 10:09:42 +05:30

418 B

chunk

Chunks an array into smaller arrays of a specified size.

Uses range to create a list of desired size. Then use map on this list and fill it with splices of arr

from math import ceil


def chunk(arr, size):
    return list(
        map(lambda x: arr[x * size:x * size + size],
            list(range(0, ceil(len(arr) / size)))))

chunk([1,2,3,4,5],2) # [[1,2],[3,4],5]