Python
dictionaries
data storage
programming
software development

Storing Python dictionaries

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Storing a Python dictionary is easy once you decide what you need from the stored data: human readability, cross-language compatibility, speed, or support for arbitrary Python objects. The right format depends less on the dictionary itself and more on who will read it later and how much trust you have in the data source.

Use JSON for Portable Structured Data

JSON is the safest default when the dictionary contains strings, numbers, booleans, lists, and nested dictionaries.

python
1import json
2
3config = {
4    "host": "localhost",
5    "port": 8080,
6    "debug": True,
7}
8
9with open("config.json", "w", encoding="utf-8") as f:
10    json.dump(config, f, indent=2)

Load it back like this:

python
1import json
2
3with open("config.json", "r", encoding="utf-8") as f:
4    config = json.load(f)
5
6print(config["host"])

JSON is readable, widely supported, and easy to version in source control. Its limitation is that it only supports JSON-compatible types.

Use pickle Only for Trusted Python Data

If the dictionary contains Python-specific objects that JSON cannot represent, pickle can serialize it directly.

python
1import pickle
2
3state = {
4    "counts": {"a": 1, "b": 2},
5    "values": [1, 2, 3],
6}
7
8with open("state.pkl", "wb") as f:
9    pickle.dump(state, f)

Read it back:

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

The warning is important: never unpickle data from an untrusted source. pickle is convenient, but it is Python-specific and unsafe for hostile input.

Use SQLite When the Dictionary Becomes Application Data

If you are storing many dictionaries, querying by fields, or updating pieces over time, a file database such as SQLite is often better than repeatedly rewriting one blob.

A simple pattern is to store the dictionary as JSON inside SQLite:

python
1import json
2import sqlite3
3
4conn = sqlite3.connect("app.db")
5conn.execute("CREATE TABLE IF NOT EXISTS documents (id INTEGER PRIMARY KEY, data TEXT)")
6
7payload = {"user": "alice", "role": "admin"}
8conn.execute("INSERT INTO documents (data) VALUES (?)", (json.dumps(payload),))
9conn.commit()

Read it back:

python
1row = conn.execute("SELECT data FROM documents WHERE id = 1").fetchone()
2if row:
3    payload = json.loads(row[0])
4    print(payload["user"])

This is a good fit when persistence starts looking more like an application database than a simple settings file.

shelve Is a Lightweight Python Option

For small Python-only tools, shelve gives you a dictionary-like persistent store.

python
1import shelve
2
3with shelve.open("cache.db") as db:
4    db["settings"] = {"theme": "light", "page_size": 20}

Later:

python
1import shelve
2
3with shelve.open("cache.db") as db:
4    print(db["settings"])

This is convenient for small scripts, but it is not a great interoperability format and still depends on Python serialization behavior under the hood.

Choose Based on the Real Requirement

A practical decision rule is:

  • choose JSON for portability and readability
  • choose pickle for trusted Python-specific object graphs
  • choose SQLite when you need querying, updates, or multiple records
  • choose shelve for quick local persistence in simple Python tools

That is a better way to think about the problem than asking for a single universally best storage format.

Common Pitfalls

  • Using pickle for untrusted data is dangerous because loading it can execute arbitrary code.
  • Choosing JSON and then expecting it to preserve arbitrary Python objects leads to serialization errors or manual conversion work.
  • Rewriting one huge file for frequently changing records becomes awkward when a small SQLite database would be a better fit.
  • Treating shelve as a cross-language storage solution is a mistake because it is mainly a Python convenience layer.
  • Forgetting text encoding when reading and writing JSON can cause avoidable problems with non-ASCII content.

Summary

  • There is no single best way to store Python dictionaries; the right format depends on portability, safety, and query needs.
  • JSON is the default choice for readable, interoperable structured data.
  • 'pickle is convenient for trusted Python-only persistence but should not be used with untrusted input.'
  • SQLite is better when the data behaves like records rather than one serialized blob.
  • 'shelve is useful for lightweight local persistence in small Python tools.'

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.