[Solved] Python – efficient way to create 20 variables?


Where is my mistake?

There are possibly three mistakes. The first is that 'variable_' + 'a' obviously isn’t equal to 'variable_1'. The second is the quoting in the argument to exec. Do

for x in list:
    exec("variable_%s=""" % x)

to get variable_a etc.

The third mistake is that you’re not using a list or dict for this. Just do

variable = dict((x, '') for x in list)

then get the contents of “variable” a with variable['a']. Don’t fight the language. Use it.

9

solved Python – efficient way to create 20 variables?