How to write a dictionary or list to a binary pickle file with Python?

In this tutorial you will learn how to save the contents of a dictionary to a pickle file (aka ‘pickling a file’). This will serialize your data objects by converting its contents into bytes. Always make sure that you only un-pickle data coming from trusted sources.

Create sample data

We will start by creating a Python list and a dictionary, which we will use in our example


# Create list object
year_lst = 2019, 2021, 2022
exp_lst = 240,340,400
rev_lst = 450, 800,700

# Create dictionary
performance_dict  = dict(year = year_lst, expenses = exp_lst, revenue = rev_lst)

Save and serialize a dictionary with pickle

This script will help you convert your dictionary to a pickle file.

import pickle

#define a patch for the pickle file on your disk
pick_path = 'C:\\temp\\perf_data.pkl'

#convert the dictionary to pickle
with open (pick_path, 'wb') as pick:
    pickle.dump(performance_dict, pick)

Note: attempting to open the file in write mode (w) instead will render a TypeError.

Load and read dictionary from pickle file

If you would like to read a pickle file into your Python program, proceed as following (remember: only read a pickle from a source you trust!)

#define an empty dictionary
performance_dict_unpacked ={}

# load the pickle contents
with open (pick_path,'rb') as pick:
    performance_dict_unpacked.update(pickle.load(pick))

Note: also here, make sure that you open the pickle file in binary mode (‘rb’) instead of read (‘r’) mode. Otherwise you will receive a UnicodeDecocodeError – as your pickle file was written as bytes not as a string.

Write and load to Pickle with Pandas

You can use the pandas library to save and convert to pickle with Pandas.

Serialize a list with pickle

Converting a Python list object to pickle can be done in a similar fashion:

pick_path = 'C:\\temp\\perf_list.pkl'

# dump a list to the pickle
with open (pick_path, 'wb') as pick:
    pickle.dump(exp_lst, pick)

# define new list
exp_lst_unpacked =[]

Note: you can also use the struct.pack() function to accomplish the same result. Make sure to import the struct library before using; to avoid a modulenotfound error.

You can de-serialize the list into a list of strings/floats in a very simple manner:

# append the pickle content to the list
with open (pick_path,'rb') as pick:
    exp_lst_unpacked.append(pickle.load(pick))

Additional questions

Why would i want to serialize a list or dictionary?

There are Several reasons for writing lists and dictionaries to binaries:

  • Based on my experience, binary files are efficient for storing complex data structures.
  • Typically, storing your data in binaries is faster (performance wise).
  • Binaries, use less space compared to text files.
  • You can use binaries to preserve your data structure and content while processing it.