How to pass the return of a function that is a tuple as arguments to another one in Python?

Asked

Viewed 153 times

2

I’m trying to return multiple values from one function to another with multiple parameters:

def chamar_ab():
    a = 1
    b = 2
    return a, b    #tupla

def soma(x, y):
    return x + y    

But using it doesn’t seem possible:

soma(chamar_ab())

How to make it work using tuple?

1 answer

4


I think that’s what you want:

def chamar_ab():
    a = 1
    b = 2
    return a, b

def soma(x, y):
    return x + y

print(soma(*chamar_ab()))

It’s the same with lists and dictionary keys, but using values looks like this:

print(soma(*chamar_ab().values()))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This operator (*) causes a sequence of data to be transposed as arguments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.