1

I'm asking the user to add an item to the list, then I'm saving the modified list. But when I'm running the program again, the previously added elements are gone. I don't understand why the new elements are saved only temporarily and my list resets itself. Could someone explain and advise how I can save new items?

import pickle

my_list = ["a", "b", "c", "d", "e"]

def add(item):
    my_list.append(item)
    with open("my_list.pickle", 'wb') as file:
        pickle.dump(my_list, file)
        return my_list


while True:
    item = input("Add to the list: \n").upper()
    if item == "Q":
        break
    else:
        item = add(item)

with open("my_list.pickle", "rb") as file1:
    my_items = pickle.load(file1)

print(my_items)
jhpratt
  • 6,841
  • 16
  • 40
  • 50
Ank12
  • 27
  • 1
  • 5

1 Answers1

1

You read the list from the file after you filled it with data. So if we analyze the main (I removed the add function, and annotated the program), we get:

# the standard value of the list
my_list = ["a", "b", "c", "d", "e"]

# adding data to the list
while True:
    # we write the new list to the file
    item = input("Add to the list: \n").upper()
    if item == "Q":
        break
    else:
        item = add(item)

# loading the list we overrided in this program session
with open("my_list.pickle", "rb") as file1:
    my_items = pickle.load(file1)

# print the loaded list
print(my_items)

So since you start with a default list, and each time you add elements to the file you rewrite the file, if you at the end of the program load the list, guess what? you obtain the list you just saved.

The solution is thus to move the loading to the top of the program:

import pickle
import os.path
# the standard value of the list
my_list = ["a", "b", "c", "d", "e"]

# in case the file already exists, we use that list
if os.path.isfile("my_list.pickle"):
    with open("my_list.pickle", "rb") as file1:
        my_items = pickle.load(file1)

# adding data to the list
while True:
    # we write the new list to the file
    item = input("Add to the list: \n").upper()
    if item == "Q":
        break
    else:
        item = add(item)

# print the final list
print(my_items)

Note that is rather inefficient to each time store the new list. You better give the user the opportunity to alter the list, and store it at the end of the program.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555