Back to Practice
#0128
Chunk a List Into Fixed Sizes
EasyPython15 min10 XP
Problem
Given a list and positive chunk size k, return a list of chunks. The final chunk may be smaller.
Why This Matters
Chunking appears in batching API calls, processing datasets, and preparing mini-batches for training.
Function Signature
def chunk_list(items, k):
Examples
Example 1
Input[1, 2, 3, 4, 5], k = 2
Output[[1, 2], [3, 4], [5]]
The leftover value 5 still becomes a final chunk.
Constraints
- Return the exact requested structure.
- Handle normal edge cases cleanly.
- Prefer readable code over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[1, 2, 3, 4, 5], k = 2
Expected[[1, 2], [3, 4], [5]]
The leftover value 5 still becomes a final chunk.
Hidden Test Categories
empty listk larger than lengthk equals one