I have a list in python that looks something like this:
list = [5, "-", 4, "*", 8]
I would like to calculate the math problem in the list so:
anwser = 5 - 4 * 8
So the variable "anwser" is -27.
I have a list in python that looks something like this:
list = [5, "-", 4, "*", 8]
I would like to calculate the math problem in the list so:
anwser = 5 - 4 * 8
So the variable "anwser" is -27.
 
    
    This is what you call a Infix notation (https://en.wikipedia.org/wiki/Infix_notation)
You can use a stack to evaluate it. I found a gist here which might help (https://gist.github.com/nava45/6333409#file-infix-expression-evaluation)
 
    
    Evaluating an equation with priority of operations requires a parser.
Fortunately, writing a parser for basic arithmetic, that is addition and multiplication, can be achieved fairly simply without the use of parsing tools.
import operator
ops = {
    '-': operator.sub,
    '+': operator.add,
    '*': operator.mul,
    '/': operator.truediv
}
def parse_mult(equation):
    equation = iter(equation)
    value = next(equation)
    for token in equation:
        if token == '*' or token == '/':
            op = ops[token]
            value = op(value, next(equation))
        else:
            yield value
            yield token
            value = next(equation)
    yield value
def parse(equation):
    equation = parse_mult(equation)
    value = next(equation)
    for token in equation:
        op = ops[token]
        value = op(value, next(equation))
    return value
equation_list = [5, "-", 4, "*", 8]
print(parse(equation_list))
-27
If you ever need to parse equations with more than two levels of priority, than an external parsing tool may become necessary.
