How to save modified list data in python? [closed]

General Tech Learning Aids/Tools 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm building a small, simple program to aid myself in studying languages and other things.

The concept is simple, I will enter a list of words, definitions, and translations etc. The script will print these items from the list one at a time, and then wait for my input of y (yes I want to delete this word from the list) or n (I don't want to delete this word from the list and append the list)

The purpose is to use it kinda like flash cards.

For example, printed output from my list for learning Spanish:

python : pitón

If i memorise the item, I will enter y to delete the item from my list. If I need more practice, I will enter n to append the item the list.

Here's the example of the code that works fine.

words = ['bacon','eggs','bread','cheese','sausage','juice','butter',]

def mylist():
    a_word = words[0]
    yes_or_no = input(a_word)

    if yes_or_no == "n":
        words.append(words.pop(0))

    elif yes_or_no == "y":
        del words[0]
x = 1
while (x < 8):
    mylist()
    x = x + 1

Example output:

>>> bacon y 
>>> eggs n 
>>> bread y 
>>> cheese n 
>>> sausage y  
>>> juice n  
>>> butter y
>>> print(words)
>>> ['eggs', 'cheese', 'juice']

So my question is: How do I get python to save the modified list, so that when I run the program again it will load the appended list?

With the example above, if I open the the program next time, the list would only contain:

['eggs', 'cheese', 'juice']

but not the original:

['bacon','eggs','bread','cheese','sausage','juice','butter']

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.