2/22/2023

How to Save and Load Python Lists using Pickle

 refer to code:


.

import pickle

# Example list
my_list = [(40, 1.646054384000001), (233, 3.0769193350000013), (221, 2.6460548819999996),
(214, 2.3542021680000005), (322, 2.726835301999998), (94, 1.201160183999999),
(193, 2.501363478000002), (171, 1.3009034040000031), (595, 5.574669749999998)]

# Save list to a file using pickle
with open("my_list.pickle", "wb") as f:
pickle.dump(my_list, f)

# Load list from the saved file
with open("my_list.pickle", "rb") as f:
loaded_list = pickle.load(f)

# Verify that the loaded list matches the original list
print(loaded_list == my_list) # True

..


thank you.

πŸ™‡πŸ»‍♂️

www.marearts.com

No comments:

Post a Comment