Open In App

How to use Pickle to save and load Variables in Python?

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Serialization is the process of converting an object’s state into a format that can be stored or transmitted and later reconstructed. Deserialization is the reverse process that convert the stored format back into an object. In Python, the pickle module provides a powerful way to serialize and deserialize Python objects, including custom classes, which formats like JSON cannot handle.

How to save variables with pickle

Pickle allows you to save Python objects so they can be reused later without losing their structure or data. This is done by converting objects into a byte stream that can be stored in memory or saved to a file.

1. Using dumps()

dumps() converts a Python object into a byte string stored in memory. It’s useful when you want to serialize data temporarily, for example, to send over a network or store in a database, without saving it to a file.

Python
import pickle
a = [{'This': 'is', 'Example': 1}, 'of', 'serialisation', ['using', 'pickle']]

res = pickle.dumps(a)
print(res) 

Output
b'\x80\x04\x95K\x00\x00\x00\x00\x00\x00\x00]\x94(}\x94(\x8c\x04This\x94\x8c\x02is\x94\x8c\x07Example\x94K\x01u\x8c\x02of\x94\x8c\rserialisation\x94]\x94(\x8c\x05using\x94\x8c\x06pickle\x94ee.'

Explanation: pickle.dumps(a) converts this list into a byte stream, effectively serializing it into a binary format that can be stored or transferred. The result is assigned to the variable res.

2. Using dump()

dump() serializes a Python object and writes it directly to a file in binary format. This method is ideal for saving data permanently on disk so it can be loaded later.

Python
import pickle
a = [{'This': 'is', 'Example': 2}, 'of', 'serialisation', ['using', 'pickle']]

with open('file.pkl', 'wb') as file:
    pickle.dump(a, file)

Output

Output
Using dump

Explanation: with open('file.pkl', 'wb') as file: opens or creates a file in write binary mode and pickle.dump(a, file) serializes the list a and saves it to that file in binary format.

How to Load Variables with Pickle

Loading variables is just as important as saving them. Pickle lets you easily restore the saved byte stream or file back into the original Python objects, allowing your program to continue working with the data seamlessly.

1. Using loads()

loads() takes a byte string that represents a serialized Python object and converts it back into the original object in memory. Use this when you have serialized data in memory and want to recover the original Python structure.

Python
import pickle
a = [{'This': 'is', 'Example': 2}, 'of', 'serialisation', ['using', 'pickle']]
s = pickle.dumps(a)

res = pickle.loads(s)
print(res)

Output
[{'This': 'is', 'Example': 2}, 'of', 'serialisation', ['using', 'pickle']]

Explanation: List a is serialized into a byte stream with pickle.dumps(a) and stored in s, then deserialized back to its original form using pickle.loads(s) and stored in res.

2. Using load()

load() reads serialized data from a binary file and reconstructs the original Python object in memory. It is used to load data previously saved using the dump() method.

Python
import pickle
with open('file.pkl', 'rb') as file:
    a = pickle.load(file)

print(a)

Output

[{'This': 'is', 'Example': 2}, 'of', 'serialisation', ['using', 'pickle']]

Explanation: This code reads a binary file using pickle.load(file), which deserializes the data from file.pkl and restores the original Python object into the variable a, then prints it.


Next Article
Article Tags :
Practice Tags :

Similar Reads