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
MKJ
  • 459
  • 6
  • 18

1 Answers1

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.

Sercan
  • 1,690
  • 2
  • 8
  • 21