How to Split a List into Chunks in Python

Split a list into fixed-size sublists using a simple list comprehension with slicing.

Easy Python 3.9+ Aug 9, 2026 Lists & loops 13 views 0 copies

Python code

8 lines
Python 3.9+
def chunk_list(lst, size):
    """Split a list into sublists of given size."""
    return [lst[i:i + size] for i in range(0, len(lst), size)]


if __name__ == "__main__":
    sample = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    print(chunk_list(sample, 3))

Output

stdout
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

How it works

range(0, len(lst), size) produces start indexes at 0, size, 2*size, etc. Slicing lst[i:i + size] extracts each chunk. The final chunk may be shorter if the list length isn't a multiple of size. This is O(n) and works for any sequence supporting slicing.

Common mistakes

  • Using `range(len(lst))` and chunking by `i // size` which is slower and more complex
  • Forgetting the last partial chunk when list length isn't divisible by size
  • Mutating the original list with `del` or `pop` instead of creating new sublists

Variations

  1. Use `itertools.zip_longest` with an iterator trick for a lazy chunking generator
  2. Use `math.ceil(len(lst)/size)` and split with a loop for very large lists to save memory

Real-world use cases

  • Batch rows from a database query into groups for paginated API responses.
  • Split a large image dataset into mini-batches for model training loops.
  • Divide a long list of email addresses into chunks for rate-limited sending.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Lists & loops

Related tutorials and quizzes for this topic.