python
list-manipulation
sublist
list-splitting
duplicate-question

Split a python list into other sublists i.e smaller lists

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Splitting a Python list into smaller sublists is usually called chunking. The right approach depends on whether you want chunks of a fixed size, a fixed number of chunks, or a lazy generator that does not build every sublist in memory at once.

Fixed-Size Chunks with Slicing

For most everyday cases, a list comprehension with slicing is the clearest solution.

python
1def chunk_list(values, size):
2    return [values[i:i + size] for i in range(0, len(values), size)]
3
4
5numbers = [1, 2, 3, 4, 5, 6, 7]
6print(chunk_list(numbers, 3))

Output:

text
[[1, 2, 3], [4, 5, 6], [7]]

This works well when:

  • the input already is a list,
  • you want all chunks immediately,
  • and the final shorter chunk is acceptable.

It is the standard answer because it is both readable and efficient enough for normal list sizes.

A Lazy Generator Version

If the input may be large, yielding chunks one at a time can be cleaner than building the whole outer list immediately.

python
1def iter_chunks(values, size):
2    for i in range(0, len(values), size):
3        yield values[i:i + size]
4
5
6numbers = [1, 2, 3, 4, 5, 6, 7]
7for chunk in iter_chunks(numbers, 3):
8    print(chunk)

This still uses slicing, but it avoids constructing the full list of chunks up front.

That is especially useful when the caller wants to process each chunk and move on rather than hold every chunk in memory.

Split Into a Fixed Number of Groups

Sometimes the requirement is not "chunks of size 3" but "split this list into 3 groups." That is a different problem.

One simple way is to distribute the elements by index:

python
1def split_into_groups(values, groups):
2    result = [[] for _ in range(groups)]
3    for index, value in enumerate(values):
4        result[index % groups].append(value)
5    return result
6
7
8numbers = [1, 2, 3, 4, 5, 6, 7]
9print(split_into_groups(numbers, 3))

This balances the groups, but the semantics are different from chunking by fixed size. Make sure the requirement is clear before choosing one or the other.

Validate the Chunk Size

A chunk size of zero or a negative value should not be accepted silently.

python
1def chunk_list(values, size):
2    if size <= 0:
3        raise ValueError("size must be greater than 0")
4    return [values[i:i + size] for i in range(0, len(values), size)]

This matters because range(0, len(values), 0) is invalid and because a negative chunk size usually means the caller supplied a bad argument.

Choose Based on the Real Requirement

A quick guide:

  • fixed-size batches: slicing with a step,
  • streaming one batch at a time: generator,
  • fixed number of groups: explicit distribution logic.

Trying to use one implementation for all three often makes the code less clear than just naming the real operation directly.

Common Pitfalls

  • Confusing "size of each chunk" with "number of chunks wanted."
  • Forgetting to validate that the chunk size is greater than zero.
  • Assuming the last chunk must have the same size as all others.
  • Building every chunk eagerly when the caller would be better served by a generator.
  • Overengineering the problem when a slicing-based list comprehension already solves the normal case cleanly.

Summary

  • Splitting a list into smaller sublists is usually called chunking.
  • The simplest fixed-size solution is a slicing-based list comprehension.
  • A generator version is useful when you want lazy processing.
  • Splitting into a fixed number of groups is a different problem from chunking by size.
  • Choose the implementation that matches the actual requirement instead of forcing one generic pattern onto every case.

Course illustration
Course illustration

All Rights Reserved.