Member-only story

5 Practical Python Programs Using the Pickle Library

Python Coding
3 min readAug 28, 2024

--

1. Saving and Loading a List

This program saves a list to a file and then loads it back.

import pickle

my_list = ['apple', 'banana', 'cherry']

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

with open('list.pkl', 'rb') as file:
loaded_list = pickle.load(file)

print("Loaded List:", loaded_list)
Loaded List: ['apple', 'banana', 'cherry']

2. Saving and Loading a Dictionary

This program demonstrates saving a dictionary to a file and loading it back.

import pickle

my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

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

with open('dict.pkl', 'rb') as file:
loaded_dict = pickle.load(file)

print("Loaded Dictionary:", loaded_dict)
Loaded Dictionary: {'name': 'John', 'age': 30, 'city': 'New York'}

--

--

Python Coding
Python Coding

Written by Python Coding

Learn python tips and tricks with code I Share your knowledge with us to help society. Python Quiz: https://www.clcoding.com/p/quiz-questions.html

No responses yet