How to save and read a list in Python

Saving a list exports the data and stores it to disk, allowing it to be accessed in future, as well as outside of the program. For example, saving a list can be used to save data that is still available the next time the program is run.

Solution for How to save and read a list in Python : You can use pickle.dump() to save a list to disk Call open(file_name, mode) with mode as “wb” or “rb” to return a writable or readable object of the file of file_name respectively. Call pickle.dump(obj, file) with the list as obj and the open file object as file to save the list to disk as the filename. To load the list from the file back into a Python object, call pickle.load(file) with the file object containing the saved list as file to access the list. Call open_file.close() to close the open_file object so that it cannot be read or written anymore.


how-to-save-and-read-a-list-in-python