Python

Pickle Dictionary in Python

Python’s pickle module is utilized to save and load data, such as dictionaries, in a binary format. This is done by serializing and de-serializing the data, which means converting it from and to Python objects in memory. To serialize data, we need to use the “pickle.dump()” method, while to read the data from the pickle “pickle.load()” method is used in Python.

This tutorial will present you with a detailed guide on writing dictionary data to a pickle file utilizing the below contents:

How to Write Dictionary Data to a Python Pickle File?

The “pickle.dump()” method is used to write the dictionary data to a pickle file.

Syntax

pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)

 

You can check the dedicated guide on the “pickle.dump()” method for further understanding.

Note: The “pickle.HIGHEST_PROTOCOL” parameter value can also be used in the “pickle.dump()” method to pick the highest protocol version. It is used when we save/load a large object.

The protocol can be either “HIGHEST PROTOCOL” or “DEFAULT PROTOCOL”, depending on the efficiency and compatibility of the pickle.

Example 1: Write Dictionary Data to a Python Pickle File

Here in this code, the “pickle” module is imported, and the dictionary data is created. Next, the “open()” function is utilized to create the pickle file and write/dump the dictionary data to the file utilizing the “pickle.dump()” method:

import pickle
dict1 = {"joseph": "anna", "lily": "henry"}
with open("new.pickle", "wb") as file:
    pickle.dump(dict1, file)
    file.close()

 

The above code writes the dictionary data to the pickle file:

To load the dictionary data back to the pickle file the “pickle.load()” method is used in Python:

import pickle
with open("new.pickle", "rb") as file:
dict1 = pickle.load(file)
file.close()
print(dict1)

 

The dictionary data has been retrieved successfully from the pickle file:

Example 2: Write Nested Dictionary Data to a Python Pickle File

We can also write the nested dictionary data to a Python pickle file. Here, the “pickle” module is imported, and the dictionary is initialized. After that, the “open()” function is used along with the “pickle.dump()” method to write the nested dictionary data. In the end, we use the “pickle.load()” method to read the nested dictionary data from the newly created pickle file.

import pickle
dict1 = {
    'ID_NO 1': {
        'Name': "Joseph",
        'Age' :20,
    },
    'ID_NO 2': {
        'Name':'Henry',
        'Age':23,
    },
    'ID_NO 3': {
        'Name':'Anna',
        'Age':24,
    }
}
with open("new.pickle", "wb") as file:
    pickle.dump(dict1, file)

with open("new.pickle", "rb") as file:
    loaded_dict = pickle.load(file)
print(loaded_dict)

 

The nested dictionary data has been read from the pickle file:

Conclusion

In Python, the “pickle.dump()” method of the “pickle” module is utilized to write the simple and nested dictionary data to the pickle file. After writing the dictionary data to the pickle file, the “pickle.load()” method is utilized in Python. This guide offered a comprehensive/complete overview of the pickle dictionary in Python.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.