0

I have a list that contains a calculation

list = ['(', '1', '+', '(', '2', '*', '3', ')', ')']

and my goal is to solve it. The answer is 7, but how can I write a code to calculate it for me? I've tried to convert the list to a string and then make it an integer so that Python would calculate it like a regular calculation but I've figured that it doesn't work like that.

a = int(''.join(list)) # this doesn't work

In other words, I'd like to convert it to this form so that Python could calculate it.

a = (1+(2*3))
print(a) # 7

Is there any easy way to solve this in Python?

sohvi
  • 11
  • FYI, you should never give a variable a name that is the same as a built-in name, like `list`. Because then if you tried to use the built-in `list()` function, you would get an error. – Random Davis Feb 08 '22 at 16:55
  • `l = ['(', '1', '+', '(', '2', '*', '3', ')', ')']; a = eval(''.join(l))` – Talha Tayyab Feb 08 '22 at 16:59

0 Answers0