The best way to save lists and/or dictionaries is as a json file.
in your code, this would be done as follows:
in your code, this would be done as follows:
import json fruit = ["Apple", "Orange", "Banana", "Mandarin"] # this can be accessed by index: print('fruit[0]: {}, fruit[2]: {}'.format(fruit[0], fruit[2])) print('fruit: {}'.format(fruit)) # To save to file named fruit.dat: with open('fruit.dat', 'w') as fout: json.dump(fruit, fout) # To load back in: with open('fruit.dat') as fin: newfruit = json.load(fin) print('newfruit: {}'.format(newfruit))output:
Output:fruit[0]: Apple, fruit[2]: Banana
fruit: ['Apple', 'Orange', 'Banana', 'Mandarin']
newfruit: ['Apple', 'Orange', 'Banana', 'Mandarin']