Chunk a list
Input
chunk_iterable([1,2,3,4,5], 2)
Output
[[1, 2], [3, 4], [5]]
The list is split into chunks of size 2. The final chunk contains the leftover element.
Full lesson preview
Split any iterable into consecutive fixed-size chunks. The last chunk may be smaller.
Problem statement
Task
Examples
Input
chunk_iterable([1,2,3,4,5], 2)
Output
[[1, 2], [3, 4], [5]]
The list is split into chunks of size 2. The final chunk contains the leftover element.
Input format
Output format
Constraints
Samples
Input
chunk_iterable('abcdef', 3)
Output
[['a', 'b', 'c'], ['d', 'e', 'f']]
Strings are iterables; chunks are lists of characters.