Explain Codes LogoExplain Codes Logo

How can I use pickle to save a dict (or any other Python object)?

python
pickle
data-serialization
object-persistence
Alex KataevbyAlex Kataev·Jan 13, 2025
TLDR

Save any Python object using pickle:

import pickle # Our object for demonstration, which could be anything though obj = {'example': 'data'} # Saving 'obj' to 'data.pkl' in binary with open('data.pkl', 'wb') as f: pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL) # Using our car's SPORT mode

That's it! obj is now immortalized in 'data.pkl' and ready to be retrieved using pickle.load() whenever needed.

Pickle protocol: What's the deal?

Always set protocol to pickle.HIGHEST_PROTOCOL to use high efficiency mode. Think of protocol versions as the gearstick in your car. Higher one equals better performance but remember it's not backward compatible. Newer might not always be better!

import pickle # Assume 'obj' is your supersonic jet to pickle with open('data.pkl', 'wb') as f: pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL) # Like shifting your car into SPORT mode

Safety first when pickling

Handle pickle knowing that it's as risky as crossing the street with your eyes closed. Yup, never unpickle data that came from the stranger's van. Use try-except to catch those moments when pickling decides to throw a tantrum. Behind those exceptions may lay fiendish bugs or worrisome data corruption:

import pickle # Assume 'file_path' is something so secretive that even we don't know what's in there try: with open(file_path, 'rb') as f: covert_data = pickle.load(f) except (FileNotFoundError, EOFError, pickle.UnpicklingError): print("Uh oh! Failed to unpickle the object. Retry from checkpoint?")

Pickle alternatives: Diverse like ice cream flavors

Tired of eating pickle every day? Try out different ice cream flavors! JSON for language-friendly text data, CSV for tidy tabular data, or HDF5 and MessagePack when you want to go all out on complex data structures. Also, JSON, XML are great when you're sending data to your non-pythonic friends across the internet.

Don't try to push an elephant into a fridge

Despite its flexibility, pickle has its sizes. In Python 3.4, pickle stops helping you after 2 GB. For pickling the un-picklable like functions, or when lambda plays hard to get, dill or cloudpickle have some wild tricks up their sleeves.

From freezing to thawing: Using pickle

Use pickle to freeze and store objects, instances, or complex hierarchies deep in your freezer (hard drive). When it's time to defrost, pickle.load() will hand you back the exact same stuff you've frozen. And don't forget to tag your tupperware! .p or .pickle lets you remember what's the frozen soup for the future.

Pitfalls: The thrill of pickling

There are things even pickle can't swallow. Just like you can't swallow a whole watermelon, pickle can't serialize file handles, network streams, or database connections. And remember to always close the refrigerator, or you risk spoilage, freezer burn and increased electric bills.