1

I am trying to get the result of two lists depending on the operator in another separate column in Python.

Here is my code

up = ["123", "1", "7685", "9987"]
down = ["45", "46", "435", "2"]
op = ["+", "-", "+", "*"]

for u, d, o in zip(up, down, op):
    #print(u, o, d)
    #print(int(u) + int(d))
    print(int(u), o , int(d))

I can print out but what I want is to get the results. This is the output I am looking for.

168 -45 8120 19,974
1
  • Append to a list? Commented Jul 6, 2020 at 12:46

1 Answer 1

2

You need to use eval() method to evaluate any string as math expression:

up = ["123", "1", "7685", "9987"]
down = ["45", "46", "435", "2"]
op = ["+", "-", "+", "*"]

for u, d, o in zip(up, down, op):
    print(eval(u + o + d))

Related doc page is here.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.