Skip to content Skip to sidebar Skip to footer

Python 3: Lists Dont Change Their Values

So I am trying to change a bunch of list items by a random percentage using a for loop. import random as rdm list = [1000, 100, 50, 25] def change(): for item in list:

Solution 1:

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]
defchange(lyst):
    for i, item inenumerate(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 )

Solution 2:

You need to store the computed value back to the list.

So replace:

for item in list:
    item = item + item*rdm.uniform(-10, 10)

With:

for index, item in enumerate(list):
    list[index] = item + item*rdm.uniform(-10, 10);

With the modified loop, you will be looping through each element in the list with its index. You can now assign back the computed value to that particular index in the list.

Solution 3:

using functional approach and list comprehensions instead of for loop, you can create a new list with the changed items (unless you NEED to modify the existing list in place):

import random as rdm
items = [1000, 100, 50, 25]

defchange(items, multiply_min=-10, multiply_max=10):
    return [i * rdm.uniform(multiply_min, multiply_max) for i in items]

items = change(items)
print(items)

Post a Comment for "Python 3: Lists Dont Change Their Values"