Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

Is there a way to expand a Python tuple into a function - as actual parameters?

For example, here expand() does the magic:

tuple = (1, "foo", "bar")

def myfun(number, str1, str2):

       return (number * 2, str1 + str2, str2 + str1) myfun(expand(tuple)) # (2, "foobar", "barfoo")

I know one could define myfun as myfun((a, b, c)), but of course there may be legacy code. Thanks

1 Answer

0 votes
by (106k points)

To expand a Python tuple into a function - as actual parameters, you can use the following method:-

myfun(*tuple) 

Example:

tuple = (1, "foo", "bar")

def myfun(number, str1, str2):

  return (number * 2, str1 + str2, str2 + str1)

myfun(*tuple)

This gives the same output that you want.

Related questions

Browse Categories

...