Save dict to pickle

Tags: #python #pickle #file #save #data #io
Author: Kaushal Krishna
Description: This notebook saves a dictionary to pickle object. Saving a dictionary using pickle is a quick and easy process. With just a few lines of code, you can store your dictionary data in a binary format that can be easily loaded later on.


Import libraries

import pickle

Setup Variables

  • data: a dictionary containing the data to be saved in the json
  • output_path: pickle output
# Inputs
data = {"name": "John Doe", "age": 30, "city": "New York"}
# Outputs
output_path = "data.pkl"


Save dictionary as pickle file

Using the pickle.dump() function, the data dictionary can be saved to a pickle file.
with open(output_path, "wb") as f:
pickle.dump(data, f)


Display result

print("The pickle file has been saved to:", output_path)