Python Forum
How do I permanently change a list after user's input? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do I permanently change a list after user's input? (/thread-13765.html)



How do I permanently change a list after user's input? - Ablazesphere - Oct-31-2018

Need Help Pray Pray


RE: How do I permanently change a list after user's input? - buran - Oct-31-2018

Could you elaborate what does permanently change a list means?
Also it's good to show some example code what you try to do so that we can be helpful.


RE: How do I permanently change a list after user's input? - Ablazesphere - Oct-31-2018

(Oct-31-2018, 07:41 AM)buran Wrote: Could you elaborate what does permanently change a list means?
Also it's good to show some example code what you try to do so that we can be helpful.

lst = ["Laptop","Phone","Python"]
user = input("Enter an element : ")
lst.append(user)
print(lst)
#I want "lst" to be updated when i restart the program. 



RE: How do I permanently change a list after user's input? - buran - Oct-31-2018

To save information between separate runs you need to store information somewhere, e.g. database, simple text file, etc. Then when you run your script - read from storage, process the information in your script, e.g. - add, delete, update, etc. Depending on storage type you may need/want to update continuously while script runs, but in any case you need to save the information at least once just before before the script ends in order to store information for future use in a next run.


RE: How do I permanently change a list after user's input? - Ablazesphere - Oct-31-2018

(Oct-31-2018, 10:15 AM)buran Wrote: To save information between separate runs you need to store information somewhere, e.g. database, simple text file, etc. Then when you run your script - read from storage, process the information in your script, e.g. - add, delete, update, etc. Depending on storage type you may need/want to update continuously while script runs, but in any case you need to save the information at least once just before before the script ends in order to store information for future use in a next run.

Please show me if you have any idea to update a program permanently.


RE: How do I permanently change a list after user's input? - Gribouillis - Oct-31-2018

(Oct-31-2018, 12:47 PM)Ablazesphere Wrote: Please show me if you have any idea to update a program permanently.
You need to use a module for data persistence. There are the pickle module or the json module in the standard library, but you can also use third party modules such as tinydb
from tinydb import TinyDB, Query
record = Query()
db = TinyDB('db.json')
if db.search(record.name == 'lst'):
    lst = db.search(record.name == 'lst')[0]['value']
else:
    lst = ["Laptop", "Phone", "Python"]
    db.insert({'name': 'lst', 'value': lst})
user = input("Enter an element : ")
lst.append(user)
db.update({'value': lst}, record.name == 'lst')



RE: How do I permanently change a list after user's input? - ichabod801 - Oct-31-2018

You can also just use open to create a new file and write to it.