Hard disks store data in the form of bits and bytes. Similarly, data is sent over the network in the form of bits and bytes. Therefore, in order to store programming language objects on a disk or send them over a network, you’ll naturally need to convert them into bits and bytes.

The process of converting data into bits and bytes is called serialization. The process of reading serialized data and converting it to object format is called deserialization. In Python, pickling and unpickling are used to serialize and deserialize objects.

What is Pickling and Unpickling

Pickling is a process of serializing objects in Python. Unpickling is the reverse process. With unpickling, you can convert serialized data back to Python objects. You can pickle and unpickle data in Python using the Python Pickle module. In this tutorial, you’ll see how to pickle and unpickle data using this module.

Pickling and Unpickling with Pickle module

The Python Pickle module contains methods that can be used to pickle and unpickle objects in Python. Depending on the destination of the serialized data, the Pickle module provides the dump() and dumps() methods for pickling. For unpickling, the load and loads() methods are used. Let’s see these four methods in detail with the help of a couple examples.

Pickling via the dump() Method

The dump() method in Python is used to pickle objects and store them on a disk. The dump() method takes two parameters. The first parameter is the object to be pickled and the second parameter is the file path that stores the pickled object.

In the following example, we create a list of some famous car manufacturers. Next, a file is opened with wb permissions which stands for write-binary. In order to store a pickle object to file, you need to open the file path with write-binary permissions. Next, the car_list object and the file path is passed to the dump() method which pickles the object.

import pickle

car_list = ["Honda", "Toyota", "Kia", "Mercedes", "Ford", "BMW"]
car_pickle = open ("E:/Datasets/car_pickle_file", "wb")
pickle.dump(car_list, car_pickle)

To unpickle a pickled object stored on a disk, you need to use the load() method. However, before that let’s first try to read the contents of the pickled file like a normal file using the open() method.

car_contents = open("E:/Datasets/car_pickle_file", "rb").read()
print(car_contents)

The following output confirms that the pickled object has stored data in the form of bytes.

Output:

b'\x80\x03]q\x00(X\x05\x00\x00\x00Hondaq\x01X\x06\x00\x00\x00Toyotaq\x02X\x03\x00\x00\x00Kiaq\x03X\x08\x00\x00\x00Mercedesq\x04X\x04\x00\x00\x00Fordq\x05X\x03\x00\x00\x00BMWq\x06e.'

Unpickling via the load() Method

As we said earlier, the load() method can be used to unpickle the pickled Python object. You have to first open the pickled file using rb (read-binary) permission and pass the opened file to the load() method, as shown below. The load() method unpickles the data and returns the actual object. The unpickled object is printed on the console.

car_pickle = open ("E:/Datasets/car_pickle_file", "rb")
car_contents = pickle.load(car_pickle)
print(car_contents)

In the output below, you can see contents of the actual car_list object that was pickled in the previous section. As expected, it matches the original list from before we pickled it.

Output:

['Honda', 'Toyota', 'Kia', 'Mercedes', 'Ford', 'BMW']

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

Pickling via the dumps() Method

The dumps() method also pickles the data. However, instead of writing the pickled object to disk, the dumps() method returns a string that contains serialized or pickled object contents. Here’s an example.

import pickle

car_list = ["Honda", "Toyota", "Kia", "Mercedes", "Ford", "BMW"]
car_list = pickle.dumps(car_list)
print(car_list)

Output:

In the script above, the car_list object has been pickled into a string which is printed on the console. The pickled content looks like this.

b'\x80\x03]q\x00(X\x05\x00\x00\x00Hondaq\x01X\x06\x00\x00\x00Toyotaq\x02X\x03\x00\x00\x00Kiaq\x03X\x08\x00\x00\x00Mercedesq\x04X\x04\x00\x00\x00Fordq\x05X\x03\x00\x00\x00BMWq\x06e.'

Unpickling via the loads() Method

You can use the loads() method to unpickle an object that is pickled in the form of a string using the dumps() method, instead of being stored on a disk via the the dump() method.

In the following example the car_list object that was pickled to a car_list string is unpickled via the loads() method.

import pickle

car_list = pickle.loads(car_list)
print(car_list)

In the output below, you can see the unpickled car_list object.

Output:

['Honda', 'Toyota', 'Kia', 'Mercedez', 'Ford', 'BMW']

Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit