Beginner Python Exercises

Split a List into Chunks in Python

Break a list into groups of 3 with slicing and range(): perfect for pages, batches and grids.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Stepping through indexes with range(0, n, size)
  • Slicing each chunk
  • Handling the shorter last chunk
Chunks of 3 from [1 ... 7]
10
2
3
43
5
6
76

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.

From egg cartons to code
size = 3🥚 Each carton holds three eggs
range(0, len(values), size)📍 Where each carton starts: 0, 3, 6
values[index:index + size]📦 Fill one carton from that point
[7]🥚 The last carton is only partly full
Slicing never fails when the last chunk is short.
In one sentence

Chunking splits a sequence into consecutive pieces of a fixed size, where the last piece may be smaller.

Key points

  1. range(0, len(values), size) gives the start index of each chunk: 0, 3 and 6.
  2. values[index:index + size] takes up to three items from that position.
  3. The last slice, values[6:9], simply returns [7] instead of raising an error.
  4. values itself 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

Run code →
main.py
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)
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Stepping the range by 1

range(len(values)) starts a chunk at every index, so chunks overlap.

Problem
[values[index:index + size] for index in range(len(values))]
Fix
[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