Python
pickle
serialization
dictionary
save-object

How can I use pickle to save a dict or any other Python object?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The standard Python way to save a dictionary or another Python object to a file is the pickle module. Pickling serializes the object into bytes, and unpickling reconstructs the object later.

For ordinary local persistence, the workflow is simple: open a file in binary mode, call pickle.dump to save, and call pickle.load to restore. The main caution is security: never unpickle data from an untrusted source.

Saving An Object With pickle.dump

Here is the basic pattern for a dictionary:

python
1import pickle
2
3data = {
4    "name": "Ada",
5    "score": 42,
6    "tags": ["python", "serialization"],
7}
8
9with open("data.pkl", "wb") as f:
10    pickle.dump(data, f)

Two details matter:

  • use binary write mode, wb
  • pass the object and the file handle to pickle.dump

The same pattern works for many Python objects, not just dictionaries.

Loading The Object Back

To restore the object, open the file in binary read mode and call pickle.load:

python
1import pickle
2
3with open("data.pkl", "rb") as f:
4    restored = pickle.load(f)
5
6print(restored)

After loading, restored is a normal Python object again.

Use A Modern Protocol

If you want to be explicit, use the highest available protocol:

python
with open("data.pkl", "wb") as f:
    pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)

That usually gives a more efficient binary format than older protocol versions.

What pickle Is Good For

pickle is convenient when:

  • both writing and reading happen in Python
  • you want to preserve Python-specific structures
  • the data is trusted

It is not the best choice when you need interoperability with other languages or a human-readable format. For that, formats such as JSON are usually better.

Security Warning

This is the most important rule: do not unpickle untrusted data. Unpickling can execute code during object reconstruction. That means a .pkl file from an unknown or untrusted source is not the same thing as a harmless text file.

For local data you created yourself, pickle is fine. For external input, treat it as executable-risk data rather than as plain storage.

pickle Is Not A Database

pickle is great for simple persistence, caching, and local experiments, but it is not a queryable storage layer. If the application needs indexing, concurrent writers, or cross-language access, a database or a structured interchange format is usually a better long-term choice.

Multiple Objects In One File

pickle can also write more than one object to the same file by calling dump repeatedly and then reading them back in order. That is occasionally useful for simple local workflows, but it works only when the reader knows the write order in advance.

Binary Mode Is Required

Pickle data is bytes, not text. That is why the file modes are wb and rb, not plain text modes.

pickle is simplest when both writer and reader are ordinary Python code.

Common Pitfalls

  • Opening the file with text mode instead of binary mode.
  • Forgetting that pickle is Python-specific and not a general exchange format.
  • Unpickling data from an untrusted source.
  • Assuming every object can be pickled cleanly, even when it holds open files or process-local state.
  • Using pickle when JSON would be simpler and safer for plain data.

Summary

  • Save a dictionary or object with pickle.dump(obj, file).
  • Load it back with pickle.load(file).
  • Use binary modes: wb for writing and rb for reading.
  • 'pickle is convenient for trusted Python-to-Python persistence.'
  • Never unpickle data you do not trust.

Course illustration
Course illustration

All Rights Reserved.