operation = ['add','subs']
num_list1 = [[2,4],[7,4],[8,4]]
def calc(op, x,y):
if op=='add':
return x+y
elif op =='subs':
return x-y
def preview():
result =[]
for x,y in num_list1:
for op in operation:
result.append([op, calc(op,x,y)])
return result
result_preview = pd.DataFrame(preview())
result_preview
And The output I get is:
But I want the output to be like:
Please help me
Why don't you do a one-liner comprehension without even having to define a calc()
function:
output = [[op] + [x + y if op == "add" else x - y for x, y in num_list1] for op in operation]
Then you can print the inner lists:
for l in output:
print(l)