In python how can i change the value of references contained inside a list ?
For example in the following code
x = 'stack'
y = 'exchange'
l = [x,y]
l[1] = 'overflow'
The last line would actually replace y with 'overflow' but what I want to do is change the reference contained at l[1] to 'overflow' how can I achieve the same ?
PS : y = 'overflow' is not an accepted answer 😛
The context probably will make the problem clearer there are 50 variable that I have inside a list initialised to zero and I want them to set their values at run-time.so that when at a later point of time I do a print variable50 , i get the new value not zero.
You do rebind reference at
l[1]to'overflow'withl[1] = 'overflow'. If you want to actually modify the string object, you can’t. Strings are immutable.