easy +10 pts

Memory view slice

Slice a list of integers into non-overlapping sublists of exactly the given chunk size.

Write a function `chunk_list(items, chunk_size)` that takes a list of integers `items` and a positive integer `chunk_size`. The function should return a list of sublists, each containing exactly `chunk_size` elements from `items`, taken in order. If `len(items)` is not divisible by `chunk_size`, the final sublist will contain the remaining elements (which will be fewer than `chunk_size`). If `items` is empty, return an empty list. Assume `chunk_size` is always greater than 0.

Constraints

0 <= len(items) <= 1000 1 <= chunk_size <= 1000 Items are integers.

Example

['>>> chunk_list([1, 2, 3, 4, 5, 6], 2)\n[[1, 2], [3, 4], [5, 6]]', '>>> chunk_list([1, 2, 3, 4, 5], 3)\n[[1, 2, 3], [4, 5]]', '>>> chunk_list([], 2)\n[]']
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about iterating over `items` with a step of `chunk_size`.
Use a loop that goes from index 0 to len(items) in increments of chunk_size.
You can append the slice `items[i:i+chunk_size]` to a result list.
The last slice will automatically be shorter if needed.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.