Hello.

I have a list like this:

mylist = [7, "+", 2, "+", 1]

I want to take them out of the list inorder to run the operation and get the real result; something like this:

result = 7 + 2 + 1

So the result variable will be set to 10.
How can i do that?!

You are trying to evaluate an arithmetic expression. There are several levels of understanding of your question.

1) If you only want a working solution, you can use python's own expression evaluator:

result = eval(' '.join(str(x) for x in mylist))

this is a little bit dangerous if somebody else can send a list to your program: it could be used to evaluate arbitrary code on your computer.

2) If you are writing your own expression mini-language and your intention is to write expressions which the python interpreter does not understand, then you need a parser which is exactly this: a machine to evaluate expressions obeying a certain grammar. I recommend using wisent in this case. It is a very simple and efficient parser. But there are many similar libraries in python.

3) You may also develop your own method for parsing expressions. You can probably design a method for simple expressions such as the one in your question. Try to use list operations and tests to determine what to do. However, if you want to extend this to more complex expressions, you will have to learn a bit of the theory of parsing languages, which is a well known and well understood domain. Don't reinvent the wheel!

Thank you @Gribouillis. The code you gave was the exact thing i was looking for. Of course i will work more on parser and wisent. And also will try to develop my own method.

Now here i have a problem. Look at this:

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
result = eval(' '.join(str(x) for x in mylist))
print result

If you run this you will get 19, the result will be set to 19.
If you use your pc calculator and enter this:

6/3+9+8+1/2

And then press = button, calculator will show you number 19.5 as the result.

But if you use your pc calculator and enter like this:

6/3

And then press = button and after that enter +9 and again press = button and go on, the last number that calculator will show you will be number 10.

I know what is the reason. Calculator do operation step by step when we add numbers step to step into calculator, but when we enter all the numbers and signs togather; calculator run multiplication first, after that division, then addition and finally subtraction. So the result will be different.

Now as i'm creating a simple calculator and using the code i put above, how can i fix this problem. With the code above, my calculator will run operations with this rule */+- and the result will be number 19. How can i get the result as number 10.

I don't know i explained my meaning clear enough or not.

And let me explain something more. in my calculator (wich is a Tkinter button) when we press a button of a number, it's number will be append into the list, then we press a operator button and the sign will be append to the list and go on. Finally when we press = button, the code i posted in my last post, will run to calculate the list and print the result.

Use () to change the order:

mylist = [6, '/', 3, '+', 9, '+', 8, '+', 1, '/', 2]
result = eval(' '.join(str(x) for x in mylist))
print(result)  # --> 19


mylist = ['(', 6, '/', 3, '+', 9, '+', 8, '+', 1, ')', '/', 2]
result = eval(' '.join(str(x) for x in mylist))
print(result)  # --> 10

Very basic math rule!

Yes @HiHe i know that. But the problem is that i'm trying to create a simple calculator. Every time i press a number-button, it's number will be appended to a list. And finally when i press = button, the list will go to run all math operation i have enterd their operator signs into that list. So i can't put any () into my list.

Or..... maybe i could define a method of mine for that! I will work on this part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.