Split into Chunks

Large lists are often easier to process in smaller parts, like showing products page by page or sending data in batches. The function chunk_list receives a list nums and splits it into consecutive sublists with at most size elements each. The last chunk may be shorter if there are not enough elements left.

Примеры

Ввод
([1, 2, 3, 4, 5], 2)
Вывод
[[1, 2], [3, 4], [5]]
Ввод
([1, 2, 3], 1)
Вывод
[[1], [2], [3]]
Ввод
([], 3)
Вывод
[]

Нужно реализовать

def chunk_list(nums: list[int], size: int) -> list[list[int]]:

Теги

lists
PostgreSQLv16

Siia ilmub koodi käivituse tulemus

Visible tests
1
Input
[1,2,3,4,5], 2
Expected[[1,2],[3,4],[5]]
2
Input
[1,2,3], 1
Expected[[1],[2],[3]]
3
Input
[], 3
Expected[]
4
Input
[1,2,3,4], 4
Expected[[1,2,3,4]]
5
Input
[1,2,3], 5
Expected[[1,2,3]]
6
Input
[1,2,3], 4
Expected[[1,2,3]]
Focus radio
Paused · SomaFM · Fluid