2

Is it possible to use a single data structure to pass into a function with multiple arguments? I'd like to do something like the following but it doesn't appear to work.

foo_bar = (123, 546)

def a(foo, bar):
    print(foo)
    print(bar)

Is it possible to do something like the following:

a(foo_bar)

instead of:

a(foo_bar[0], foo_bar[1])

What is the pythonic way of doing this?

0

1 Answer 1

16

What you want is:

a(*foo_bar)

See Unpacking Argument Lists in the tutorial for details… but there really isn't much more to it than this.

For completeness, the reference documentation is in Calls:

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.