List Chunks

Imagine splitting a long list into smaller portions, such as showing products page by page or sending data in batches. Write a function chunk that receives a list items and a chunk size size. Split the list into consecutive chunks of that size. 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(items: list, size: int) -> list[list]:

Теги

listsarray
PostgreSQLv16

Tady se objeví výsledek běhu kódu

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,4,5,6], 3
Expected[[1,2,3],[4,5,6]]
6
Input
[1], 5
Expected[[1]]
Focus radio
Paused · SomaFM · Fluid