HOWTO · Python
How to Save a Dictionary to a File in Python
This tutorial demonstrates how to save a dictionary to a file in Python
On this page
To save a Python dictionary and read it later, choose a format based on how you will use the data: use JSON for JSON-compatible values when you want a portable, human-readable file; use pickle for Python objects when the file will only be loaded from a trusted source; or use NumPy’s .npy format when your workflow already depends on NumPy. Each example below writes a file and reads the dictionary back.
Save a Dictionary to File in Python Using the dump Function of the pickle Module
The pickle module stores Python objects in a Python-specific binary format. The following example writes a dictionary with pickle.dump() and reads it with pickle.load(). Use wb to write bytes and rb to read them; the temporary directory keeps the example self-contained.
import pickle
from pathlib import Path
from tempfile import TemporaryDirectory
my_dict = {"Apple": 4, "Banana": 2, "Orange": 6, "Grapes": 11}
with TemporaryDirectory() as directory:
file_path = Path(directory) / "myDictionary.pkl"
with file_path.open("wb") as file:
pickle.dump(my_dict, file)
with file_path.open("rb") as file:
new_dict = pickle.load(file)
print(new_dict)
{'Apple': 4, 'Banana': 2, 'Orange': 6, 'Grapes': 11}
Only load pickle files from sources you trust: unpickling can execute code. Pickle is useful when Python-specific serialization is acceptable, but it is not a portable interchange format.
Save a Dictionary to File in Python Using the save Function of NumPy Library
If your project already uses NumPy, np.save() can store a dictionary in an .npy object array. Loading that object data requires allow_pickle=True; call .item() on the loaded zero-dimensional array to retrieve the dictionary. This also carries the trust risk of loading pickled data. NumPy must be installed.
import numpy as np
from pathlib import Path
from tempfile import TemporaryDirectory
my_dict = {"Apple": 4, "Banana": 2, "Orange": 6, "Grapes": 11}
with TemporaryDirectory() as directory:
file_path = Path(directory) / "dictionary.npy"
np.save(file_path, my_dict)
new_dict = np.load(file_path, allow_pickle=True).item()
print(new_dict)
{'Apple': 4, 'Banana': 2, 'Orange': 6, 'Grapes': 11}
Save a Dictionary to File in Python Using the dump Function of the json Module
For a readable file that can be exchanged with other languages, use Python’s built-in json module when the dictionary contains JSON-compatible values. json.dump() writes to a text file, and json.load() reads it back. A context manager closes each file even if an operation fails. JSON does not preserve arbitrary Python objects or every Python type; for example, custom classes need an explicit conversion strategy.
import json
from pathlib import Path
from tempfile import TemporaryDirectory
my_dict = {"Apple": 4, "Banana": 2, "Orange": 6, "Grapes": 11}
with TemporaryDirectory() as directory:
file_path = Path(directory) / "myDictionary.json"
with file_path.open("w", encoding="utf-8") as file:
json.dump(my_dict, file)
with file_path.open("r", encoding="utf-8") as file:
new_dict = json.load(file)
print(new_dict)
{'Apple': 4, 'Banana': 2, 'Orange': 6, 'Grapes': 11}