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
Inicia sesión para ver tu historial de envíos
Iniciar sesiónInicia sesión para usar el AI Mentor
Iniciar sesiónVisible tests
1
Input
[1,2,3,4,5], 2Expected
[[1,2],[3,4],[5]]2
Input
[1,2,3], 1Expected
[[1],[2],[3]]3
Input
[], 3Expected
[]4
Input
[1,2,3,4], 4Expected
[[1,2,3,4]]5
Input
[1,2,3], 5Expected
[[1,2,3]]6
Input
[1,2,3], 4Expected
[[1,2,3]]Focus radio
Paused · SomaFM · Fluid