As hinted in the comments, the proper way of mapping names to values dynamically is using a dictionary:
d = dict((i, 'this is your value: {}'.format(i)) for i in tableVal)
print d['val1']
Output:
this is your value: val1
You can add this mapping to globals()
so your names become global variables:
globals().update(d)
print val1
Output:
this is your value: val1
But this is almost always a bad idea. Other people will not expect variables being magically created and they might shadow pre-existing variables. You can mitigate the risk by putting them in their own namespace:
class Namespace(object):
def __init__(self, d):
vars(self).update(d)
n = Namespace(d)
print n.val1
This allow you to use autocomplete in an IDE or the IPython shell without polluting your global namespace.
An even better alternative using the standard library, if immutability is OK, is collections.namedtuple
:
n = namedtuple('Namespace', tableVal)(*d.values())
print n.val1