def each_slice(arr, n):
return [arr[i:i + n] for i in range(0, len(arr), n)]
print(each_slice([1, 2, 3, 4, 5], 2))
print(each_slice([1, 2, 3, 4, 5], 3))
print(each_slice([1, 2, 3, 4, 5], 4))
# => [[1, 2], [3, 4], [5]]
# => [[1, 2, 3], [4, 5]]
# => [[1, 2, 3, 4], [5]]
参考: