I got a list
v = ["a", "b", "c"]
and i want to turn into
v = [a, b, c]
to use the entries as variables. I know that eval() and exec() are not recommended. How can i solve that instead?
I got a list
v = ["a", "b", "c"]
and i want to turn into
v = [a, b, c]
to use the entries as variables. I know that eval() and exec() are not recommended. How can i solve that instead?
If it's a global variable you can use
v = [ globals()[i] for i in ['a', 'b', 'c'] ]
For example...
a = 1
b = 2
c = 3
v = [ globals()[i] for i in ['a','b','c'] ]
print(v)
>>>[1, 2, 3]
If it's a local variable in the code block for a separate function/class you can use locals
instead. Of course, as mentioned in the comments you should avoid this scenario at all costs in the first place.