19
Pickling and Unpickling in Python🥒
In Layman's terms, Pickling is a process to convert your python objects into a file.
Technically speaking, it is a way to convert your well-known python objects like lists, sets, tuples, dictionaries, strings, pandas dataframes,... to a character stream. And that character stream contains all the information required to reconstruct that python object in another script.

❗Note Pickling ≠ Compression → They both are completely different!
For all those programming nerds 🤓 out there, The process of converting an object to a file is also known as Serialization or Marshalling or Flattening.
Retrieving that object back from the file is known as DeSerialization or Unmarshalling or Unflattening.
Python provides the
pickle
module for performing serialization/deserialization.Let's say you want to pickle a dictionary into a file,
pickle
,
import pickle
#A Sacred Dictionary 😌
sacred_dict = {"name":"Gaitonde", "location":"Chand 🌙" ,
"side-kick":"Bunty" }
Let's see how to use it.
Here are the simple steps,
Here are the simple steps,
pickle.dump()
by passing the dictionary and file object.
"""
w => Write mode
b => Binary mode
wb => Write in binary mode
"""
with open("sacred.pkl", "wb") as f:
pickle.dump(sacred_dict, f)
sacred_dict
in form of the character stream.That's it, You have stored your dictionary in a file! Easy Peasy 😁
Now Let's see how to unpickle or retreive that dictionary back.
The .pkl extension is just a convention that helps us identify it as a pickle file.
To retrieve the object back, we have to use the
pickle.load()
method passing the file object of the pickled file,"""
r => Read mode
b => Binary mode
rb => Read in Binary mode
"""
with open("sacred.pkl", "rb") as f:
retreived_dict = pickle.load(f)
#Let's print retreived_dict to confirm
print(retreived_dict)
#Output
#{'name': 'Gaitonde', 'location': 'Chand 🌙', 'side-kick': 'Bunty'}
That doesn't mean, you should not use pickle
module. Just make sure you trust the source of that pickle file.
19