0

I have a list of variables, where I would like to change the values. I tried this but apparently doesn't work

a,b,c=1,2,3
for i in [a,b,c]:
    i=1
print(a,b,c) #1 2 3

Actual case: I wanted to make it more concise as they are similar, only node.left and node.right is different. This is a part of a string to binary tree.

temp=array.pop(0)
if not temp=='':
    array, node.left=deserialize(array, Node(temp), False)
temp=array.pop(0)
if not temp=='':
    array, node.right=deserialize(array, Node(temp), False)
if isFirst: return node
return array, node
Jason Adhinarta
  • 98
  • 1
  • 4
  • 7
  • 3
    How close is this example to your actual case? Do you want to initialize all values as 1? – Luke Sawczak Dec 06 '18 at 13:24
  • Why would you want to do this in a such manner? This is because you are creating a list from this variables which is a different object and change the values of it. – Rarblack Dec 06 '18 at 13:38

4 Answers4

4

you can try this

def trial():
  number_list = [1, 2, 3]

  for item, value in enumerate(number_list):
      number_list[item] = 1

  print(number_list)

if __name__ == "__main__":
  trial()
Aybars
  • 395
  • 2
  • 11
  • 1
    Personally, I would use `range(len(number_list))` to get the index, seeing as there is no need for `value`. – BoobyTrap Dec 06 '18 at 13:40
0

Your list of object contains integer objects. In Python a name is mapped (I believe it is the word) to the object. When the FOR loop is executing the name i is mapped to a copy of the integer object that is mapped by a (b and c successively). So in fact you are changing a copy and not the original object. If i am not mistaken.... Another thing, if you do the following you'll notice that x is a list of integers and not a list of variable:

a,b,c=1,2,3
x=[a,b,c]
x
    [1, 2, 3]
a=0
x
    [1, 2, 3]
Toady
  • 357
  • 2
  • 13
  • so how to change the original? – Jason Adhinarta Dec 06 '18 at 13:51
  • if you want to change a you have to assign it a new integer to the variable but the list x (in my example) is not going to change. Can you explain exactly what you try to achieve? Perhaps as one of the answer states a dictionary might be the answer. – Toady Dec 06 '18 at 14:02
0

For a variable number of named variables, use a dictionary:

d = dict(zip('abc', range(1, 4)))  # {'a': 1, 'b': 2, 'c': 3}
d = {k: 1 for k in d}              # {'a': 1, 'b': 1, 'c': 1}

If you only care about the end result, just use dict.fromkeys:

d = dict.fromkeys('abc', 1)        # {'a': 1, 'b': 1, 'c': 1}

Then use d['a'], d['b'], etc to retrieve values.

jpp
  • 159,742
  • 34
  • 281
  • 339
0

I feel like the answers have no really answered your question properly.

Firstly, lists are mutable, this means that their contents can be changed at will. Integers are immutable, this means their contents cannot be changed. This is a simplification, for further reading on this see this.

[a, b, c] - This constructs a list with the instantaneous values of a, b, and c. When you iterate over this list you can change the contents of the list because the list is mutable, however, the integers themselves do not change as they are immutable. What Python does here (behind the scenes) is create a new integer object of value 1, and switch the pointer to that new object.

As a result, when you ask for the original values of a, b, and c, they are unchanged - because they cannot be changed, only replaced. Here is an example code block that demonstrates this better hopefully:

immutable_int = 1
mutable_list = [immutable_int]
for i in range(len(mutable_list)):
    # mutate the list by changing the pointer of the element
    mutable_list[i] = 2

print(immutable_int)
print(mutable_list)

This prints the following:

>>> 1
>>> [2] 

In essence, the list can be changed in place, it is still the same list and still points to the same piece of memory. When we change the integers the previous integers we used are unchanged because integers are immutable, and so new integers must be created.

In order to achieve what you desire, you can do a number of things, the first thing I can think of is just initialise them as a list and use them like that, like so:

change_list = [1, 2, 3]

for i in range(len(change_list)):
    change_list[i] = 1

print(change_list)

Let me know if my answer is incomplete or you are still confused.

Adam Dadvar
  • 384
  • 1
  • 7