I have a list like this
yy = ['A1', 'B1', 'C1']
with the values for A1
, B1
and C1
in a dict like this
ff = {
'A1': 10,
'B1': 20,
'C1': 30
}
Now I want to do sum of the list with values from ff
. This is what I tried to do
p = "sum(lst)"
eval(p, {'lst': yy}, ff)
But I get TypeError: unsupported operand type(s) for +: 'int' and 'str'
.
During debugging I found out that if I do like this p = "sum([A1, B1, C1])"
and eval It works. Not sure why this happens?
Full code:
ff = {
'A1': 10,
'B1': 20,
'C1': 30
}
yy = ['A1', 'B1', 'C1']
p = "sum(lst)"
eval(p, {'lst': yy}, ff)
I know eval
's consequences. I filter everything before it reaches eval