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.

list

Mutable, ordered, indexable sequence.

list
tuple

Immutable, ordered sequence.

tuple
dict

Insertion-ordered key-value map.

dict
set

Mutable collection of unique hashable values.

set
frozenset

Immutable, hashable set.

frozenset
range

Lazy integer sequence.

range

Records#

Fixed-shape data with named fields.

Records

namedtuple, @dataclass, TypedDict. The three ways to give a fixed-shape value a name.

Records

Specialized containers#

The collections module covers the gaps in the built-ins.

deque

Double-ended queue. O(1) at both ends.

deque
defaultdict

Dict that auto-initialises missing keys with a factory.

defaultdict
Counter

Multiset / frequency map.

Counter
OrderedDict

Ordered dict with move_to_end and order-aware equality.

OrderedDict
ChainMap

Layered dict lookup, leftmost first.

ChainMap

Heap and queues#

Heap

heapq: binary min-heap on a list. Priority queues and top-N extraction.

Heap
Queues

queue.Queue, multiprocessing.Queue, asyncio.Queue for producer-consumer.

Queues

Binary buffers#

struct

Pack and unpack fixed-layout binary records.

struct
memoryview

Zero-copy view over a bytes-like object.

memoryview
BytesIO / StringIO

In-memory file-like objects.

BytesIO and StringIO
Bitwise integers

Bit operations on arbitrary-precision int.

Bitwise integers

Tabular (third-party)#

Pandas containers; third-party (pip install pandas) but the operator’s default for any tabular workload that fits in memory.

Series

pandas.Series: 1-D labelled array. Index, values, dtype.

Series
DataFrame

pandas.DataFrame: 2-D labelled table. Rows of records, columns of Series.

DataFrame

The wider ecosystem; reach for these when pandas does not fit the workload.

Library

When to reach for it

pandas

The default. Eager, single-threaded, in-memory. Largest documentation and recipe surface.

polars

Faster than pandas (Rust + Arrow); supports a lazy query planner; first choice for any workload that pegs pandas on a single machine.

pyarrow

Apache Arrow in Python. The columnar interchange format (zero-copy across pandas, polars, duckdb, Parquet, Spark); also a standalone tabular layer.

duckdb

In-process SQL engine that reads Arrow / pandas / polars directly. The right tool when the question reads as SQL.

ibis

Backend-portable DataFrame API; one Python expression compiles to duckdb, BigQuery, Postgres, Snowflake, polars.

modin

Drop-in import modin.pandas as pd replacement that parallelises across cores and a Ray / Dask cluster.

dask.dataframe

Partitioned pandas across a cluster; lazy graph; larger-than-memory workloads on commodity boxes.

vaex

Memory-mapped, out-of-core; billion-row exploration on a laptop.

pyspark.sql.DataFrame

Apache Spark from Python; cluster-scale; the production choice when the data exceeds a single host.

Choosing#

Choosing

The operator’s cheat-sheet for picking the right container.

Choosing

References#