Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...