Skip to content Skip to sidebar Skip to footer

How To Assign Values To Variables In A List In Python?

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

Solution 1:

you can try this

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

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

if __name__ == "__main__":
  trial()

Solution 2:

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]

Solution 3:

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: 1for 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.

Solution 4:

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 inrange(len(mutable_list)):
    # mutate the list by changing the pointer of the element
    mutable_list[i] = 2print(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] = 1print(change_list)

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

Post a Comment for "How To Assign Values To Variables In A List In Python?"