-7

Suppose i want to add something to

list = []

such that the value of list gets updated in the code it self. In runtime: list gets modified to

list =['hello','Dude']

How can i do so?

What i mean is, that there are real changes made to the List value in the .py file.

user2707082
  • 155
  • 1
  • 6
  • The [Python tutorial](http://docs.python.org/2/tutorial/datastructures.html) explains lots of basic stuff like this – dbr Aug 30 '13 at 10:11
  • 1
    I would not recommend attempting to do this. It is better to separate code and data completely, and for example store your data in a `json` file that you read in, for example like this: `list = json.load(open('data.json'))` (Of course you need to `import json`). You can write it similarly: `json.dump(list, open('data.json', 'wb'))` – Magnus Hoff Aug 30 '13 at 10:18

3 Answers3

2

Just use append where ever you need it:

list = []
list.append('hello')
print list
list.append('Dude')
print list

Output:

['hello']
['hello', 'Dude']<

Easy way would be to create additional file and to store variables there.

Code:

list = []
f = open("list.txt", "r+")
for item in f:
    list.append(str(item).rstrip())

f.write("Something")
f.close()

list.txt:

hello
Dude

list.txt after execution:

hello
Dude
Something
4d4c
  • 8,049
  • 4
  • 24
  • 29
  • No. What i mean is. If in the code i have List = ''. After runtime the code changes to some List = ['hi','there'] – user2707082 Aug 30 '13 at 10:13
  • 2
    Post code that you have – 4d4c Aug 30 '13 at 10:14
  • I will begin making the code once you tell me how to make real changes to the .py file from the code. – user2707082 Aug 30 '13 at 10:15
  • I am confused... do you want to edit the list or do you want to edit your .py script in another .py script? – Nils Werner Aug 30 '13 at 10:16
  • I want to edit some List value in the .py script so that whatever i add remains permanent, and the next time i run the script, i can work with whatever my List had from the first run of the program. – user2707082 Aug 30 '13 at 10:18
  • You don't realize data persistence by modifying the code. Save the data to a data file and load it if you need to. – Matthias Aug 30 '13 at 10:25
2

Judging from your comments to one of the other answers what you are looking for is a way to serialize and save an object to a file so you can reload it once you re-run the program. This is done using pickle.

An example of this can be found on stack overflow: How to save an object in Python:

import pickle

try:
    with open('list.pk', 'rb') as input:
        list = pickle.load(input)
except:
    list = []

list.append('something')
print(list)

with open('list.pk', 'wb') as output:
    pickle.dump(list, output, pickle.HIGHEST_PROTOCOL)
Community
  • 1
  • 1
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
1

The only way to do it is rewriting the .py file.

You don't really want to do that even if it's indeed technically possible.

You should instead keep your data in a separate file, loaded when starting the program and saved back when exiting.

There are special modules for storing python values like e.g. shelve.

A very common alternative is storing the data in a format that can be understood even by other languages intead of python objects, e.g. in a relational database like sqlite.

6502
  • 112,025
  • 15
  • 165
  • 265