Dev Helpers

Python / Lists and tuples

Chunk a list

Dev Helpers · August 21, 2025

Chunk boundaries are easier to inspect when the final short chunk remains intact. Slices naturally stop at the end of the list.

Worked example

values = list(range(7))
print([values[i:i+3] for i in range(0, len(values), 3)])

Result

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

Implementation notes

A list slice creates a new list containing the same element references. Copy nested mutable records only when edits to the batch must remain isolated.

Try it yourself

Process seven values in batches of three.

Open the Python example →

Editorial date August 21, 2025 · Published December 6, 2026

Related articles

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.

Flatten one level

A one-level flatten turns a sequence of groups into a sequence of members. Deeper lists remain members in their own right.