Caleb Howard Caleb Howard - 7 months ago 15
Python Question

How to I modify an external list?

I'm trying to code this thing in python, and I have bumped into a road block. I have this list, and I want to modify it, and keep the changes after the program is exited. I have no clue where to start.

Essentially, I want a list that can be accessed and modified, and will be saved and not reset after the thing using it is closed.

EDIT: Here is my idea; There is a list that I have, let's call it basket.
I want to code a program to add to and remove from the 'basket', and to keep the changes after I close the program. Hope that clarifies... And I tried using global, but that got all funny, so I essentially deleted that version.


You can use json, pickle, or any similar library to write data to a file and read it back and store in a list. This will create the file save.json in your current working directory that contains json-information about your list. You will notice that your list will always contain the numbers you added before, it doesn't "forget" what you added to the list since it's saved in a file.

import os
import json

foo = [1,2,5,9] #default

#if safe file exists, load its contents into foo list
if os.path.isfile("save.json"): 
    with open("save.json", "r") as file:
        foo = json.loads(

foo.append(int(input("Enter a number to add to foo")))

#write foo list to file
with open("save.json", "w") as file: