What are pickling and unpickling in Python?

In this shot, we will discuss pickling and unpickling in Python.

Pickle is a module in Python that is primarily used to serialize and de-serialize a Python object structure. Both pickling and unpickling become essential when we have to transfer Python objects from one system to another.

  • Pickling is a process by which the object structure in Python is serialized. A Python object is converted into a byte stream when it undergoes pickling.

  • Unpickling is a process by which original Python objects are retrieved from the stored string representation i.e., from the pickle file. It converts the byte stream into a Python object.

Code

To understand this better let’s look at the code snippet below.

import pickle
my_list = {15, 'Python', 'Hello World'}
# Pickling
with open("data.pickle","wb") as file_handle:
pickle.dump(my_list, file_handle, pickle.HIGHEST_PROTOCOL)
# Unpickling
with open("data.pickle","rb") as file_handle:
retrieved_data = pickle.load(file_handle)
print(retrieved_data)

Explanation

  • In line 1, we import the pickle module in Python.

  • In line 2, a demo Python list is created which will be pickled.

  • In lines 4 and 5, pickling is done. A .pickle file is created and the Python object (here demo Python list) is dumped.

  • In lines 7 to 9, we have unpickled the pickled file which thereby gives the demo Python list that was created as an output.

Note :

  • The dump() function is used to serialize an object hierarchy.
  • pickle.HIGHEST_PROTOCOL is an integer value that represents the highest available protocol version.
  • The load() function is used to de-serialize a data stream.

In this way, we can use pickling and unpickling in Python. It is a tool of immense importance when we want to preserve our work for later use, especially due to the fact that it can serialize almost any Python object without any boilerplate.

Free Resources