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)