Search code examples
pythonparameterstuplesiterable-unpacking

How can I explode a tuple so that it can be passed as a parameter list?


Let's say I have a method definition like this:

def myMethod(a, b, c, d, e)

Then, I have a variable and a tuple like this:

myVariable = 1
myTuple = (2, 3, 4, 5)

Is there a way I can pass explode the tuple so that I can pass its members as parameters? Something like this (although I know this won't work as the entire tuple is considered the second parameter):

myMethod(myVariable, myTuple)

I'd like to avoid referencing each tuple member individually if possible...


Solution

  • You are looking for the argument unpacking operator *:

    myMethod(myVariable, *myTuple)