Data Structures#
The containers the operator stores work in. Python’s built-in
types cover most everyday needs; collections, heapq,
queue, array, and struct cover the specialized cases.
Picking the right structure up front is usually the difference
between a recipe that runs in seconds and one that times out on
real data.
For the primitive and composite types themselves, see Types. For the algorithms that run over them, see Algorithms.
Built-in#
The everyday types are documented under Types.
Mutable, ordered, indexable sequence.
Immutable, ordered sequence.
Insertion-ordered key-value map.
Mutable collection of unique hashable values.
Immutable, hashable set.
Lazy integer sequence.
Records#
Fixed-shape data with named fields.
namedtuple, @dataclass, TypedDict. The three
ways to give a fixed-shape value a name.
Specialized containers#
The collections module covers the gaps in the built-ins.
Double-ended queue. O(1) at both ends.
Dict that auto-initialises missing keys with a factory.
Multiset / frequency map.
Ordered dict with move_to_end and order-aware equality.
Layered dict lookup, leftmost first.
Heap and queues#
heapq: binary min-heap on a list. Priority queues
and top-N extraction.
queue.Queue, multiprocessing.Queue,
asyncio.Queue for producer-consumer.
Binary buffers#
Pack and unpack fixed-layout binary records.
Zero-copy view over a bytes-like object.
In-memory file-like objects.
Bit operations on arbitrary-precision int.
Tabular (third-party)#
Pandas containers; third-party (pip install pandas) but the
operator’s default for any tabular workload that fits in memory.
pandas.Series: 1-D labelled array. Index, values, dtype.
pandas.DataFrame: 2-D labelled table. Rows of records,
columns of Series.
The wider ecosystem; reach for these when pandas does not fit the workload.
Library |
When to reach for it |
|---|---|
The default. Eager, single-threaded, in-memory. Largest documentation and recipe surface. |
|
Faster than pandas (Rust + Arrow); supports a lazy query planner; first choice for any workload that pegs pandas on a single machine. |
|
Apache Arrow in Python. The columnar interchange format (zero-copy across pandas, polars, duckdb, Parquet, Spark); also a standalone tabular layer. |
|
In-process SQL engine that reads Arrow / pandas / polars directly. The right tool when the question reads as SQL. |
|
Backend-portable DataFrame API; one Python expression compiles to duckdb, BigQuery, Postgres, Snowflake, polars. |
|
Drop-in |
|
Partitioned pandas across a cluster; lazy graph; larger-than-memory workloads on commodity boxes. |
|
Memory-mapped, out-of-core; billion-row exploration on a laptop. |
|
Apache Spark from Python; cluster-scale; the production choice when the data exceeds a single host. |
Choosing#
The operator’s cheat-sheet for picking the right container.
References#
Types for the primitive and composite types.
Algorithms for the algorithms over them.
Syntax for slicing and comprehensions.
Python Standard Library, Data Types — the reference for
collections,heapq,queue,array,struct,io,copy,enum,types.Time complexity wiki — CPython container performance.