How to Serialize a Python Dict into a String and Back?


serialize/deserialize python

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 Dictionary 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'x80x04x95!x00x00x00x00x00x00x00}x94(x8cx05Hellox94]x94(Kx01Kx02Kx03eK*x8cx05Worldx94u.'

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'x80x04x95!x00x00x00x00x00x00x00}x94(x8cx05Hellox94]x94(Kx01Kx02Kx03eK*x8cx05Worldx94u.'

# 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 Python Dict – The Quick and Dirty Way

To serialize a Python dict using no external dependency, 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.

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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here