PYTHON

Python Pickle: Syntax, Usage, and Examples

The pickle module in Python lets you serialize and deserialize Python objects, meaning you can convert them to a byte stream and back again. This is especially useful for saving program state, caching data, or transferring Python objects between different executions.

Pickle Python objects with just a few lines of code. You can store dictionaries, lists, sets, custom classes, and more—all in a compact binary format.


What Is Pickle in Python?

Pickling in Python refers to the process of converting a Python object into a byte stream using the pickle module. This process is also called serialization. The reverse—converting the byte stream back into an object—is called unpickling.

data = {"name": "Alice", "age": 30}
with open("data.pkl", "wb") as f:
    pickle.dump(data, f)

This example demonstrates how to use the python pickle dump method to store a dictionary.


Why Use Python Pickle

Pickle lets you store Python data structures directly to a file without needing to convert them into text formats like JSON or CSV. It supports more complex objects, such as custom classes and nested data structures, which makes it useful for prototyping, caching, and storing trained machine learning models.


Basic Syntax of Pickle Python

To serialize (pickle) an object:

pickle.dump(obj, file)

To deserialize (unpickle) an object:

obj = pickle.load(file)

Use binary file modes (wb, rb) to work with pickled data.


Python Pickle Example

Pickling

import pickle

user = {"username": "johndoe", "email": "john@example.com"}
with open("user.pkl", "wb") as file:
    pickle.dump(user, file)

Unpickling

with open("user.pkl", "rb") as file:
    loaded_user = pickle.load(file)
print(loaded_user)

This is a simple example of how to create and load a pickle file Python program might use to store temporary data.


Pickling Custom Classes

You can pickle custom objects easily:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("Alice", 30)
with open("person.pkl", "wb") as f:
    pickle.dump(person, f)

Then you can load the pickle file Python created:

with open("person.pkl", "rb") as f:
    loaded_person = pickle.load(f)

Pickling in Python: Limitations

  • You cannot pickle open file handles or database connections.
  • Pickled data is not secure; avoid loading pickle files from untrusted sources.
  • Python version mismatches may cause unpickling errors.

Using Pickle with Protocols

Pickle supports several protocols (versions of the serialization format). By default, it uses the latest version:

pickle.dump(obj, file, protocol=pickle.HIGHEST_PROTOCOL)

Use this when sharing pickled data between different Python versions.


Loading Pickle File Python Programmatically

You can automate loading data in apps or data pipelines:

import pickle

def load_model():
    with open("model.pkl", "rb") as file:
        model = pickle.load(file)
    return model

This pattern is common in machine learning workflows.


Pickling in Python Example with Nested Structures

complex_data = {
    "name": "Example",
    "scores": [90, 85, 88],
    "attributes": {"height": 170, "weight": 65}
}

with open("complex.pkl", "wb") as f:
    pickle.dump(complex_data, f)

This shows pickling in Python working with nested dictionaries and lists.


When Not to Use Pickle

  • Avoid it when sharing data across different programming languages.
  • Avoid using it in public-facing applications that load external pickle files.
  • Use other formats like JSON or CSV if human readability or portability is more important.

Best Practices for Using Python Pickle

  • Always open files in binary mode when using pickle.
  • Store version information with your data if it’s long-lived.
  • Use with open() blocks to manage file resources properly.
  • Don’t load pickle files from untrusted sources due to security risks.
  • Use the highest protocol for best performance and compatibility.

The Python pickle module gives you a flexible way to serialize and store objects across sessions. You’ve seen how to create, load, and manage pickle files in real-world scenarios. Pickling in Python is especially useful for applications involving large objects, trained models, and temporary session storage.

Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster