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
  Unable to access the user input value given to daterangepicker klllmmm 1 360 May-26-2021, 09:16 PM
Last Post: nilamo
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 277 May-24-2021, 08:48 AM
Last Post: bowlofred
  Problem restricting user input in my rock paper scissors game ashergreen 6 688 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Looking for a way to loop until user enters from a list? PythonW19 7 472 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  LinkedList by User Input SantiagoPB 5 810 Mar-03-2021, 09:54 PM
Last Post: deanhystad
  User input/picking from a list AnunnakiKungFu 2 431 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  Generate Random operator, take user input and validate the user mapypy 4 467 Feb-03-2021, 08:41 PM
Last Post: nilamo
  Add file to sys.path permanently hcccs 5 668 Jan-31-2021, 11:26 AM
Last Post: hcccs
  LIST or ARRAY Comparison and change of value nio74maz 0 272 Dec-21-2020, 05:52 PM
Last Post: nio74maz
Question dict value, how to change type from int to list? swissjoker 3 540 Dec-09-2020, 09:50 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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