Unpacking Arguments
Learn how can we enhance function calls by unpacking lists and dictionaries, optimizing argument provision, and dictionary merging.
We'll cover the following...
There’s one more nifty trick involving positional and keyword parameters. We’ve used it in some of our previous examples. Now let’s have a look at its explanation.
Example
Given a list or dictionary of values, we can pass a sequence of values into a function as if they were normal positional or keyword arguments. Have a look at this code:
Press + to interact
def show_args(arg1, arg2, arg3 = "THREE"):return f"{arg1 = }, {arg2 = }, {arg3 = }"
The function accepts three parameters, one of which has a default value. But when we have a list of three argument values, we can use the *
operator inside a function call to unpack it into the three ...