Python objects can be saved (or serialized) as pickle files for later use. In this tutorial, we will look at how to save a Python dictionary as a pickle file with the help of some examples.
How to write Python dictionary to a pickle file?

You can use the Python pickle
module’s dump()
function to serialize a dictionary to a pickle file. The following is the syntax –
import pickle # save dictionary to pickle file with open('my_filename.pickle', 'wb') as file: pickle.dump(my_dict, file, protocol=pickle.HIGHEST_PROTOCOL)
It saves the object as a pickle file which you can later use.
Exercise caution when working with pickle files. The pickle module is not secure. Only unpickle data you trust. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. See here.
Let’s look at some examples of using the above syntax –
For example, you have a dictionary storing the names to department mappings of employees in an office. You want to save this information in a pickle file so that it can be used later.
import pickle # create a dictionary employees = { "Jim": "Sales", "Dwight": "Sales", "Angela": "Accounting" } # save dictionary to pickle file with open("employee_info.pickle", "wb") as file: pickle.dump(employees, file, pickle.HIGHEST_PROTOCOL)
The above code saves the dictionary employees
to the file employee_info.pickle
in our current working directory.
You can also deserialize a pickle file to get back your object using the pickle
module. Use the pickle.load()
function.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s read the same dictionary from its pickle file that we saved above.
# laod a pickle file with open("employee_info.pickle", "rb") as file: loaded_dict = pickle.load(file) # display the dictionary print(loaded_dict)
Output:
{'Jim': 'Sales', 'Dwight': 'Sales', 'Angela': 'Accounting'}
We get our original dictionary with all its content loaded from its pickle file.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.