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.
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.
Load it back like this:
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.
Read it back:
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:
Read it back:
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.
Later:
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
picklefor trusted Python-specific object graphs - choose SQLite when you need querying, updates, or multiple records
- choose
shelvefor 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
picklefor 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
shelveas 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.
- '
pickleis 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.
- '
shelveis useful for lightweight local persistence in small Python tools.'
Related reading
- Strategy to find duplicate entries in a binary search tree
- Streaming messages from one Kafka Cluster to another
- Stretching out an array
- String Matching Computing the longest prefix suffix array in kmp algorithm
- ''str'' object does not support item assignment
- ''str'' object has no attribute ''decode'' for Tensorflow in Python
- String permutations rank data structure
- String search in string array in objective c

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 courseTrack 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.