How to update a list of variables in python?

12,168

Solution 1

It's not possible if you store immutable items in a list, which Python integers are, so add a level of indirection and use mutable lists:

a,b,c,d = [1],[2],[3],[4]
L = [d,b,a,c] # user-defined order
print L
a[0] = 5
print L

Output:

[[4], [2], [1], [3]]
[[4], [2], [5], [3]]

This has the feel of an X-Y Problem, however. Describing the problem you are solving with this solution may elicit better solutions.

Solution 2

you need to update the list and not the variable:

a = 1
b = 2
lst = [a, b]  # the list now stores the references a and b point to
              # changing a later on has no effect on this!  
lst[0] = 2

and please do not use list as variable name! this is a keyword built-in type in python and overwriting it is a very bad idea!

if you only know the value of the element in the list (and this value is unique) you could do this:

old_val = 2
new_val = 3

lst[lst.index(old_val)] = new_val

Solution 3

Why don't you use dictionary??

With dictionary:

_dict = {}
_dict["a"] = 1
_dict["b"] = 2
print _dict["a"] # prints 1

Now if you want to set and get value of variable "a":

_dict["a"] = 2
print _dict["a"] # prints 2
Share:
12,168

Related videos on Youtube

john hon
Author by

john hon

Updated on May 26, 2022

Comments

  • john hon
    john hon almost 2 years

    This is an aggravating issue that I've come into whilst programming in python. The user can append various variables to a list however, this list when accessed later on will still display the same results as when first assigned. e.g below

    a=1
    b=2
    list = [a,b] #user defined order
    print list # 1,2
    a=2
    print list # prints 1,2
    

    I need list to print out 2,2. However i cannot find out a way to DYNAMICALLY update the list to accommodate ANY order of the variables (many ways are hard coding which i've seen online such as assigning list = [a,b] when needed to update however i dont know if its b,a or a,b)

    Help would be much appreciated. Thank you

    Edit : My question is different due to being about varaibles that need to be dynamically updated in a list rather than simply changing a list item.

    • ThisSuitIsBlackNot
      ThisSuitIsBlackNot over 7 years
      Possible duplicate of Passing a list element as reference
    • AChampion
      AChampion over 7 years
      Don't use list as a variable name, python has a list type which this would hide.
  • ThisSuitIsBlackNot
    ThisSuitIsBlackNot over 7 years
    Of course, this means you can't do math on your objects without writing a whole bunch of other code.
  • user508402
    user508402 over 7 years
    Doing maths is not mentiooned anywhere by the OP. Your comment, while true, is therefore irrelevant. Don't vote me down for it.
  • ThisSuitIsBlackNot
    ThisSuitIsBlackNot over 7 years
    When somebody fills a list with numbers, they will almost certainly want to manipulate those numbers somehow. With your solution, you can't even compare the list items to other numbers to see if they're equal! I don't think that's a very useful approach.
  • user508402
    user508402 over 7 years
    it answers the question rather than conjectures.
  • john hon
    john hon over 7 years
    that is my issue, i CANNOT simply state lst = [a,b] because the order is unknown so as much as i want to, i cannot do that :(
  • hiro protagonist
    hiro protagonist over 7 years
    then your problem is not the same as the one you present in the question: there you put a first in the list and then want to change a (the first element in the list). what is the question then?
  • john hon
    john hon over 7 years
    sorry for the confusion. a could be the nth element of the list and the list could be defined in any order e.g [a,b...],[b,a....] etc. so my question is how to a change a variable that is inside a list and then get it to reflect this later? thanks for your help so far
  • john hon
    john hon over 7 years
    sigh sadly it was sorta like an XY problem, however i found another case where i did actually need this so your efforts were not wasted ! :)
  • rishi jain
    rishi jain almost 4 years
    hello @hiroprotagonist, if the list if list of pandas dataframes and I want to change actual value of dataframe, how will I change this equation? I dont want value to just change in list (lst) but the actual values in dataframes also change? Any ideas?
  • hiro protagonist
    hiro protagonist almost 4 years
    @rishijain sorry, it is a bit hard to understand what you really want. why don't you make it a real question so people more proficient than me can answer! good luck!
  • rishi jain
    rishi jain almost 4 years
    @hiroprotagonist, in the above example we have a =1 and b =2, and both are variables and lst is a list of a and b. In case, a and b are both dataframes and lst is a list of dataframes, how can I assign new values of a and b to lst, such that the actual values of a and b change?
  • hiro protagonist
    hiro protagonist almost 4 years
    if you change your dataframe inplace, the change should be reflected in the original variables as well. see e.g. stackoverflow.com/questions/47245583/… .