Python Forum
How do I permanently change a list after user's input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I permanently change a list after user's input?
#1
Need Help Pray Pray
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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. 
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
(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')
Reply
#7
You can also just use open to create a new file and write to it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 928 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  save values permanently in python (perhaps not in a text file)? flash77 8 1,120 Jul-07-2023, 05:44 PM
Last Post: flash77
  Change font in a list or tuple apffal 4 2,635 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  restrict user input to numerical values MCL169 2 869 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,030 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 1,837 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to change the datatype of list elements? mHosseinDS86 9 1,900 Aug-24-2022, 05:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020