I have a bunch of variables, and a JSON file containing keys with the names of the same variables. I would like to be able to go through my list of variables and assign them values based on the corresponding entries in the JSON file. Is there a good way to do this? I have been looking around for a while but haven't come across what I am looking for.
Asked
Active
Viewed 21 times
0
-
Check `locals` (or `globals`) – rdas Oct 29 '21 at 18:45
-
1You should use a dictionary instead of variables. – nadapez Oct 29 '21 at 18:49
-
3Basically, that's a bad idea. Use a proper data structure, like dict. And because you already have the JSON (deserialized) you may not need to create another dict. in For a more extended discussion, have a look at [Keep data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) and [Why you don't want to dynamically create names](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html) – buran Oct 29 '21 at 18:50
-
1And you have to post more context in the question. Post the code you have. We need more inormation like in which scope are the variables defined, how are thery defined, how many variables, which names has the variables. – nadapez Oct 29 '21 at 18:51