I am trying to create a dictionary with dictionary comprehensions in the following way (which is part of a much larger code)
columns = ['zeta', 'Lm', 'u_mean']
print('zeta', eval('zeta'))
print(locals())
dic = {col: [eval(col)] for col in columns}
The first print
prints exactly as expected (the value of the variable zeta
) and the second print
confirms that zeta
is in the locals dictionary, but in the dictionary comprehension command python fails with this error
NameError: name 'zeta' is not defined
Unfortunately, when trying to reproduce to error in order to post here, I found out that I can't reproduce the error, because the following commands work in ipython
:
zeta,Lm,u_mean=1,4,69
columns=['zeta', 'Lm', 'u_mean']
print('zeta',eval('zeta'))
print(locals())
dic={ col : [eval(col)] for col in columns }
It is only those commands inside my code that do not work. So, am I missing something? Is there some test I can do to see what's wrong?