I have a certain function (Quash) which i want to use to run over a list of variables e.g. a list called 'list'. Is there a way I can run this over the list of numbers without using eval?
NB: I have put the QUash function here only for the sake of simplicity. The really function is more complicated with lots of strings.
Is there an alternative to using the eval function in this loop to have the value extracted?
list = [1,2,3,4]
def stringfunction(Var):
AnalysisVar = "Quash.(" + str(Var) + ")"
return AnalysisVar
CompleteNames = [stringfunction(i) for element in list]
for i in CompleteNames:
eval("%s" %CompleteNames[i])
Thanks in advance.
First, it's a bad idea to name your list "list" as that name is already used in Python. Second, just update your list comprehension:
my_list = [1, 2, 3, 4]
CompleteNames = [Quash(str(i)) for i in my_list]