Below are three common, battle-tested ways to persist
dict[str, tuple[np.ndarray, …]]
Pick whichever “best” matches your constraints (size, speed, interoperability, security).
Fast & Python-only ⇒ pickle / joblib
• Great when you’ll re-load from (trusted) Python code only.
• Handles any nested structure automatically (no flattening).
python
import joblib # pip install joblib
# data is your dict[str, tuple[np.ndarray]]
joblib.dump(data, 'arrays.joblib', compress=("lzma", 3))
# later …
data_loaded = joblib.load('arrays.joblib')
Why joblib over pickle? It uses pickle under the hood but
• is a one-liner,
• adds transparent compression,
• stores large NumPy arrays efficiently (zero-copy memmap).
Drawbacks: not readable outside Python; never un-pickle untrusted files.
• Small file (zip) + no extra dependency.
• Needs a tiny “flatten/unflatten” helper because np.savez wants
name→array pairs, not tuples.
python
import numpy as np
from collections import defaultdict
# ---------- SAVE ----------
flat = {}
for k, tup in data.items():
for i, arr in enumerate(tup):
flat[f'{k}_{i}'] = arr
np.savez_compressed('arrays.npz', **flat)
# ---------- LOAD ----------
loaded = np.load('arrays.npz')
tmp = defaultdict(list)
for name, arr in loaded.items(): # e.g. 'foo_0', 'foo_1'
key, idx = name.rsplit('_', 1)
tmp[key].append(arr)
restored = {k: tuple(v) for k, v in tmp.items()}
Pros: minimal code & dependencies; readable by any tool that can unzip + NumPy.
Cons: still Python-centric structure; must flatten.
Cross-language, large data ⇒ HDF5
• Structured, random-access, chunked; readable in C, C++, Julia, R, MATLAB…
• Perfect when arrays are huge or you append later.
python
import h5py
with h5py.File('arrays.h5', 'w') as f:
for key, tup in data.items():
grp = f.create_group(key)
for i, arr in enumerate(tup):
grp.create_dataset(str(i), data=arr)
# ---------- LOAD ----------
with h5py.File('arrays.h5', 'r') as f:
data_loaded = {k: tuple(grp[str(i)][()]
for i in range(len(grp)))
for k, grp in f.items()}
Pros: language-agnostic, scalable, supports compression & chunking.
Cons: extra dependency, a bit more boilerplate.
Quick decision matrix
Need…
Pick …
Pure Python, zero fuss
joblib / pickle
Small file, only NumPy arrays
np.savez_compressed
Multi-language, TB-scale data
HDF5 (h5py)
Security against untrusted input
Anything except pickle/joblib (use HDF5 or recreate objects manually)
So: “Best” depends on your priorities, but for most day-to-day Python work a single line of joblib.dump/-load is the sweet spot; switch to HDF5 when you need portability or to np.savez_compressed when you want a simple zip file without new libraries.