How do I split a list into equally-sized chunks?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Splitting a list into chunks is a common task in Python when you need to batch API calls, process files in groups, or paginate work. The best solution depends on whether you want a list result, a lazy iterator, or strictly equal-sized groups with padding or validation.
The Basic Slicing Pattern
If you already have a list in memory, the simplest solution is slicing inside a comprehension. It is readable and works in every modern Python version.
Output:
This is usually the right answer when:
- the input is already a list
- a shorter final chunk is acceptable
- you want the result immediately
Use A Generator For Large Inputs
If the input is large, returning every chunk at once may waste memory. A generator yields one chunk at a time.
This still relies on indexing, so it is best for sequences such as lists or tuples. If you want to support any iterable, use itertools.
itertools.batched In Modern Python
Python 3.12 added itertools.batched, which is the cleanest built-in option for general iterables. It yields tuples lazily, so it works well for streams and generators.
Output:
If you are on Python 3.13 or newer, batched also supports strict=True, which raises an error when the last chunk is incomplete. That is useful when "equally sized" must be literal rather than approximate.
Padding To Force Equal Sizes
Sometimes you need every chunk to have exactly the same length, even if the final group is short. In that case, pad the remainder.
Output:
This pattern is useful when you are formatting tabular data or batching work for systems that require fixed-size blocks.
Equal Chunk Size Versus Equal Number Of Chunks
People often ask for "equally sized chunks" when they actually want "split this list into N chunks". Those are different problems.
Chunk size means:
- choose the maximum size of each group
- the last group may be smaller
Number of chunks means:
- decide how many groups you want
- distribute the elements across them
If you need a fixed number of groups, use a different approach:
That produces groups that are as balanced as possible, which is not the same as fixed chunk size.
Common Pitfalls
- Forgetting to validate the chunk size. A value of
0should raise an error. - Assuming the last chunk will always be full. Most chunking functions allow a shorter final chunk.
- Using list slicing on a general iterator. Slicing needs a sequence, while
itertools.batchedworks on any iterable. - Confusing "size of each chunk" with "number of chunks to create".
- Materializing every chunk in memory when a lazy iterator would be cheaper.
Summary
- For a normal list, slicing with a comprehension is the simplest solution.
- For large or streaming inputs, prefer a lazy approach.
- '
itertools.batchedis the modern built-in choice for general iterables.' - Use padding only when every chunk must have exactly the same length.
- Be clear whether you want fixed chunk size or a fixed number of chunks.

