Problem Formulation
Given a Python dictionary containing lists and other data structures. You want to store the dictionary in a file or send it over the network in a more efficient form.
How to serialize a Python dictionary into a string, and then deserialize the string back to a dictionary data structure?
Here’s a rough example of what you want to do:
Serialization Example Input: {'Hello': [1, 2, 3], 42: 'World'} Output: <some serialized form> Deerialization Example Input: <some serialized form> Output: {'Hello': [1, 2, 3], 42: 'World'}
So, let’s dive into the most Pythonic solution right away!
Serialize and Deserialize a Dict with Pickle
To serialize a given dictionary d
, simply import the pickle
module with import pickle
, and assign the result of pickle.dumps(d)
to a variable. This variable will then hold a serialized binary string you can use to store the dictionary on your computer or send it over the network.
import pickle d = {'Hello': [1, 2, 3], 42: 'World'} serialized_d = pickle.dumps(d)
If you print the string variable, you’ll get an unreadable binary string output representing the serialized dictionary:
print(serialized_d) # b'\x80\x04\x95!\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x05Hello\x94]\x94(K\x01K\x02K\x03eK*\x8c\x05World\x94u.'
To deserialize the binary string created by pickle.dumps()
and create a new dictionary object from it, use the pickle.loads()
function and pass the serialized binary string representation into it. The output is a new dictionary object that is different from the original unserialized dictionary.
deserialized_d = pickle.loads(serialized_d) print(deserialized_d) # {'Hello': [1, 2, 3], 42: 'World'}
To summarize, this is the most Pythonic way to serialize and deserialize a Python dictionary:
import pickle d = {'Hello': [1, 2, 3], 42: 'World'} # Serialize Dict serialized_d = pickle.dumps(d) print(serialized_d) # b'\x80\x04\x95!\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x05Hello\x94]\x94(K\x01K\x02K\x03eK*\x8c\x05World\x94u.' # Deserialize Dict deserialized_d = pickle.loads(serialized_d) print(deserialized_d) # {'Hello': [1, 2, 3], 42: 'World'}
You can confirm that the original dictionary and the deserialized dictionary are copies but don’t point to the same object using the is
operator:
print(d is deserialized_d) # False
But consider a simple no-library alternative too!
Serialize and Deserialize a Dict – Quick and Dirty No-Library
To serialize a Python dict using no external dependency and in an human-readable way, simply convert the string to a dictionary using the built-in function str()
. To deserialize this back, pass the string representation of the dict into the built-in eval()
function that will return a new dictionary object that is a copy of the original.
d = {'Hello': [1, 2, 3], 42: 'World'} # Serialize Dict serialized_d = str(d) print(serialized_d) # Deserialize Dict deserialized_d = eval(serialized_d) print(deserialized_d)
The advantage of this method is that it is simple and doesn’t need a library. The serialized dictionary is also human-readable. However, it’s very inefficient compared to the previous method because the serialized string comes with a lot of unnecessary overhead that is optimized away with pickle
.
Python eval(s)
is a built-in function that parses the string argument s
into a Python expression, runs it, and returns the result of the expression. You can watch my explainer video on this particular function here:
Further Alternatives
For comprehensibility, it should be mentioned that two alternatives exist:
Both also provide serialization/deserialization functionality.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.