Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Split a list into fixed-size chunks

Break a list into consecutive sublists of a given size. The last chunk may be smaller if elements don't divide evenly.

Python practice14 minList & Dictionary PatternsIntermediateLast updated March 20, 2026

Problem statement

Implement chunk_list(lst, size) that splits the input list into consecutive sublists (chunks) each of length size, except possibly the last chunk which may be shorter. The function should return a list of chunks. Size is guaranteed to be a positive integer. Do not modify the input list in-place.

Task

Write a function that splits a list into chunks of a fixed size using idiomatic Python.

Examples

Basic split

Input

chunk_list([1, 2, 3, 4, 5, 6], 3)

Output

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

The list divides exactly into two chunks of size 3.

Input format

A list lst and an integer size > 0.

Output format

A list of sublists (chunks).

Constraints

1 <= size; preserve element order; do not modify the original list.

Samples

Sample 1

Input

chunk_list([1,2,3,4,5], 2)

Output

[[1, 2], [3, 4], [5]]

Creates pairs; the final chunk has the remaining element.