Split a List into Chunks in Python
Break a list into groups of 3 with slicing and range(): perfect for pages, batches and grids.
- Stepping through indexes with range(0, n, size)
- Slicing each chunk
- Handling the shorter last chunk
Slice every size items
The problem
Showing 10 results per page, sending data in batches of 100 or arranging items in rows of 3 all mean splitting a list into fixed-size pieces. The last piece is often shorter, and the code must handle that without errors.
Think of it like this
Splitting a list into chunks is like packing eggs into cartons of three. You fill one carton, start the next, and so on. The last carton may only have one egg, and that is fine.
size = 3🥚 Each carton holds three eggsrange(0, len(values), size)📍 Where each carton starts: 0, 3, 6values[index:index + size]📦 Fill one carton from that point[7]🥚 The last carton is only partly fullChunking splits a sequence into consecutive pieces of a fixed size, where the last piece may be smaller.
Key points
range(0, len(values), size)gives the start index of each chunk: 0, 3 and 6.values[index:index + size]takes up to three items from that position.- The last slice,
values[6:9], simply returns[7]instead of raising an error. valuesitself is not changed.
When should you use it?
- Pagination: showing items page by page.
- Processing or sending data in batches.
- Arranging items in rows of a grid.
When not to use it
- Streams or generators that cannot be sliced: use
itertools.batched(Python 3.12+) or a loop. - Splitting into a fixed number of parts rather than a fixed size: compute the size from the length first.
Example code
values = [1, 2, 3, 4, 5, 6, 7]
size = 3
# Slice one chunk at each size-aligned position.
chunks = [values[index:index + size] for index in range(0, len(values), size)]
print(chunks)
Result of running the code
[[1, 2, 3], [4, 5, 6], [7]]
Code explanation
range(0, len(values), size) produces the start positions 0, 3 and 6, one for each chunk. values[index:index + size] slices up to three items from each start; when the slice goes past the end, Python returns whatever is left instead of failing. That is why the last chunk is [7].
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Stepping the range by 1
range(len(values)) starts a chunk at every index, so chunks overlap.
[values[index:index + size] for index in range(len(values))]
[values[index:index + size] for index in range(0, len(values), size)]
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges