-1

I store variables in a list to access them later with a for loop. My desire is to set some of the variables not just the list's entries to None

import numpy as np

a = np.random.rand(3,2)
b = np.random.rand(3,2)
c = np.random.rand(3,2)

data_lists = [a,b,c]


data_lists[:] = [None]

won't work. Does anyone know a pythonic way to do it without explicitly writing a = None and so on?

  • what is the expected output? you can't reference back the variable names – mozway Aug 19 '22 at 12:09
  • 1
    "I store variables in a list to access them later with a for loop." - then you should probably be using a list in the first place, not some unrelated variables.. – Thierry Lathuille Aug 19 '22 at 12:10
  • 2
    Variables aren't stored in the list; values are. Storing different values in the list has no effect on the values still referenced by `a` et al. See https://nedbatchelder.com/text/names.html – chepner Aug 19 '22 at 12:10
  • Integers are immutable setting new values in a list only rewrites the references, so the variables are not affected. In theory, you could do it with mutable objects (for example lists inside of your list) – Yevhen Kuzmovych Aug 19 '22 at 12:12
  • Also, does this answer your question? [Is there a way for a change in a variable in a list to be reflected in the list?](https://stackoverflow.com/questions/63566948/is-there-a-way-for-a-change-in-a-variable-in-a-list-to-be-reflected-in-the-list) – Yevhen Kuzmovych Aug 19 '22 at 12:13
  • While it is possible to do something like this, I would suggest to rethink thoroughly why do you need to that, and perhaps if there are better alternatives (you probably need to use a different data structure). – norok2 Aug 19 '22 at 12:30

1 Answers1

0

Perhaps you could do a = b = c = None, then recreate data_lists by doing data_lists = [a, b, c] again. You can't set a variable to None implicitly. Hope this helps