Python Pickle Module Tutorial – In Python, the Pickle module is a powerful tool for serializing and deserializing Python objects. Serialization, also known as pickling, is the process of converting a Python object into a byte stream, while deserialization, or unpickling, is the process of converting a byte stream back into a Python object.
This is particularly useful for saving complex data structures, such as lists and dictionaries, to a file and reloading them later. In this article, we will explore how to use the Pickle module to save and load variables in Python, with detailed explanations and coding examples.
Understanding Pickle
The Pickle module is part of Python’s standard library, which means it is available without installing any additional packages. It supports serialization of a wide range of Python data types, including integers, floats, strings, lists, dictionaries, sets, and even custom objects.
Here are the basic steps to use Pickle:
- Import the Pickle Module: You need to import the Pickle module before you can use it.
- Open a File: Open a file in binary mode for writing (
wb) or reading (rb). - Serialize (Pickle) the Object: Use
pickle.dump()to serialize the object and write it to the file. - Deserialize (Unpickle) the Object: Use
pickle.load()to read the serialized object from the file and deserialize it.
Example 1: Saving and Loading a Simple Variable
Let’s start with a simple example of saving and loading a list.
import pickle
# Example list
my_list = [1, 2, 3, 4, 5]
# Save the list to a file
with open('my_list.pkl', 'wb') as f:
pickle.dump(my_list, f)
# Load the list from the file
with open('my_list.pkl', 'rb') as f:
loaded_list = pickle.load(f)
print(loaded_list)
Output:

In this example, we first import the Pickle module and define a list called my_list. We then open a file named my_list.pkl in write-binary (wb) mode and use pickle.dump() to serialize the list and write it to the file. To load the list, we open the file in read-binary (rb) mode and use pickle.load() to deserialize the list.
Example 2: Saving and Loading a Dictionary
Next, let’s see how to save and load a dictionary.
import pickle
# Example dictionary
my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# Save the dictionary to a file
with open('my_dict.pkl', 'wb') as f:
pickle.dump(my_dict, f)
# Load the dictionary from the file
with open('my_dict.pkl', 'rb') as f:
loaded_dict = pickle.load(f)
print(loaded_dict)
Output:

Here, we follow the same steps as in the previous example, but with a dictionary. The process remains the same: we serialize the dictionary and write it to a file, then deserialize it and load it back into a variable.
Example 3: Saving and Loading a Custom Object
Pickle can also handle custom objects. Let’s create a simple class and see how to save and load its instances.
import pickle
# Define a custom class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f'Person(name={self.name}, age={self.age})'
# Create an instance of the class
person = Person('Bob', 25)
# Save the object to a file
with open('person.pkl', 'wb') as f:
pickle.dump(person, f)
# Load the object from the file
with open('person.pkl', 'rb') as f:
loaded_person = pickle.load(f)
print(loaded_person)
In this example, we define a Person class with a constructor that initializes the name and age attributes. We create an instance of the Person class and serialize it to a file using pickle.dump(). To load the object, we use pickle.load() to deserialize it from the file.
Functions used:
- In python, dumps() method is used to save variables to a pickle file.
Syntax:
pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
- In python, loads() is used to load saved data from a pickled file
Syntax:
pickle.loads(data, /, *, fix_imports=True, encoding=”ASCII”, errors=”strict”, buffers=None)
Saving a variable:
- Method 1: Passing the variable
In dumps() method, we can pass the variable, and it will return us the binary string for the same. We can then transmit it to other python modules or save in a database.
import pickle
# Create a variable
myvar = [{'This': 'is', 'Example': 1}, 'of',
'serialisation', ['using', 'pickle']]
# Use dumps() to make it serialized
serialized = pickle.dumps(myvar)
print(serialized)

- Method 2: We can directly save the variable in a file itself.
Example:
import pickle
# Create a variable
myvar = [{'This': 'is', 'Example': 2}, 'of',
'serialisation', ['using', 'pickle']]
# Open a file and use dump()
with open('file.pkl', 'wb') as file:
# A new file will be created
pickle.dump(myvar, file)
Real-World Applications of Pickle
The Pickle module is useful in various real-world applications, including:
- Saving Model States in Machine Learning: Machine learning models often require saving the state of trained models to disk and loading them later for predictions.
- Storing Session Data: Web applications can use Pickle to store session data for users, enabling persistence across different sessions.
- Data Caching: Pickle can be used to cache expensive computations or frequently accessed data, improving the performance of applications.
Conclusion
The Pickle module in Python provides a straightforward and efficient way to serialize and deserialize Python objects. By using Pickle, you can save complex data structures to a file and reload them later, making it an invaluable tool for various applications. In this article, we covered the basics of Pickle, including how to save and load simple variables, dictionaries, and custom objects, with practical coding examples.
Whether you are working on a machine learning project, a web application, or any other project that requires saving and loading data, Pickle can help you achieve your goals efficiently. Start experimenting with Pickle in your projects and see how it can simplify your data handling processes.





Leave a Reply