best-practices

Data Structure Selection

Which Python data structures compile best and which to avoid.

Published May 30, 2026

Numeric Arrays: list

Use list for homogeneous numeric sequences.

Key-Value Lookups: dict

dict is highly optimised for compiled code.

Immutable Records: tuple

tuple has lower overhead than list for read-only data.

Avoid: Mixed-Type Collections

bad = [1, "two", 3.0]  # boxed objects

Avoid: Nested Deep Structures

Shallow structures compile faster than deeply nested dicts of lists of dicts.