Your item =
…. line, as it stands, just associates a new object with the name item
in the function’s namespace. There is no reason why this operation would change the content of the list object from which the previous value of item
was extracted.
Here is a listing that changes a list in-place:
import random
lyst = [1000,100,50,25]
def change(lyst):
for i, item in enumerate(lyst):
item = item + item * random.uniform(-10, 10)
lyst[i] = item
print(lyst)
change(lyst)
print(lyst)
The lyst[i] =
… assignment is the key line that actually changes the list’s content. Of course you can collapse the two assignments into one line if you want: lyst[i] = item =
….. Or you can omit the reassignment to item
if you’re not going to use it again in the loop: lyst[i] = item + item *
…
Note that I performed two minor fixes in addition: I changed the variable name from list
to lyst
so that it doesn’t overshadow your builtin reference to the list
class. I have also altered your function so that it takes the list object as an argument, rather than relying on referring to it using a hard-coded global variable name. Both of these are just good practice; nothing to do with your problem.
Finally, note that you can do all of this much more succinctly and transparently with a so-called list comprehension. If you don’t have to modify the list in-place, i.e. if it’s OK to end up with a modified copy of the original list:
lyst = [ item + item * random.uniform(-10, 10) for item in lyst ]
If you need to modify the list in-place (e.g. if other references to the original list exist elsewhere and they, too, should point to the updated content after change()
is called) then you can follow the suggestion in Padraic’s comment:
lyst[:] = [ item + item * random.uniform(-10, 10) for item in lyst ]
In that last case, you can even save memory (which will only be a concern for very large lists) if you change the bracket shape and thereby use a generator expression instead:
lyst[:] = ( item + item * random.uniform(-10, 10) for item in lyst )
3
solved python 3: lists dont change their values