-1

Is it possible to convert a string to a variable?

For example:

a = b = c = 0
my_list = ["a", "b", "c"]

# any function or module to change my_list to below

my_list = [a, b, c]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

2

You can use eval() for referencing variables with their names as string as follows:

a = b = c = 0
my_list = ["a", "b", "c"]

#any function or module to change my_list to below

my_list = [eval(i) for i in my_list]
print(my_list)

Output

[0, 0, 0]
Cardstdani
  • 4,999
  • 3
  • 12
  • 31