Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions python-list/chunks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
def split_list(list_object, chunk_size):
chunks = []
for start in range(0, len(list_object), chunk_size):
stop = start + chunk_size
chunks.append(list_object[start:stop])
return chunks
from typing import Any, List


def split_list(list_object: List[Any], chunk_size: int) -> List[List[Any]]:
"""
Split a list into smaller chunks of a given size.

Args:
list_object: The list to split.
chunk_size: The size of each chunk (must be a positive integer).

Returns:
A list of sublists, each with at most `chunk_size` elements.

Raises:
ValueError: If chunk_size is less than or equal to zero.

Example:
>>> split_list([1, 2, 3, 4, 5], 2)
[[1, 2], [3, 4], [5]]
"""
if chunk_size <= 0:
raise ValueError("chunk_size must be a positive integer")

return [
list_object[start : start + chunk_size]
for start in range(0, len(list_object), chunk_size)
]