Home > Back-end >  Python get return values of a function separately
Python get return values of a function separately

Time:01-10

I have a simple function in python. Is it an easy way to get result_1 and result_2 in separate lists? thanks

def add(x, y):
    result_1 = x   y
    result_2 = x*y
    return result_1, result_2

for example for x=5, y=6 something like

 result_1=[11]
 result_2=[30]

CodePudding user response:

Not sure why you would need two lists with single elements but you can define whatever return format you want. I e you can create 2 lists using your results:

def add(x, y):
    result_1 = x   y
    result_2 = x*y
    return [result_1], [result_2]

This will return 2 lists, each consisting of a single element.

CodePudding user response:

You could do somthing like this to separate the return in 2 variables:

result_1, result_2 = add(5,6)

and if you want it to be inside a list follow @dc914337 answer.

  • Related