Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Argument Dictionary Packing and Unpacking

00:00 Just as you can use argument tuple packing and unpacking for dealing with positional parameters to a function, Python allows you to use something called argument dictionary packing and unpacking to do the same thing with keyword arguments.

00:18 The double asterisk (**) is used to specify when dictionary packing or unpacking is going to occur. If you use the double asterisks in front of a parameter name in the definition of a function, you are indicating that you want dictionary packing to take any number of keyword arguments provided at the function call and pack them into a dictionary where the key-value pairs of the dictionary come from the keywords for the keys and the argument values for the values when the dictionary is created.

00:55 We typically use kwargs, keyword args, for the parameter which will hold the dictionary when packed. Here is a simple function, where all we’re going to do is look at the dictionary created when this function is called using an arbitrary number of keyword arguments.

01:18 We’re going to display the dictionary, display its type, and then we’re going to iterate through the dictionary, printing out the key-value pairs one at a time.

01:28 If you want to see this, from argument_dictionary_packing we will import our function. Here, when we call our function, it’s expecting to perform dictionary packing on an unknown number of keyword arguments.

01:53 So we can say… We’ll create a keyword foo, so the first parameter we’ll call foo. We’ll set it equal to 1. Call the second parameter bar, give it a value of 2. Call the third parameter baz and give it a value of 3.

02:12 And so we can see that each of those keyword arguments were packed into a dictionary, which we see here, and then we saw that we have a dictionary object and we could run through it seeing the key-value pairs coming from the keyword and argument value.

02:33 You can also use unpacking at a function call. So, let’s recall this function,

02:43 which again is a function just taking three parameters, x, y, and z, and just printing them out.

02:52 And so let’s grab that version of f(). In a typical use, f() is expecting three parameters, and I can traditionally provide them using keyword arguments.

03:11 That would be a typical use for this particular function. So, we can define this dictionary. And again, we probably had a dictionary already created and have been doing other work with, and now we need to use this function f() that is expecting just three parameters. But our dictionary has the key 'x', which maps to 'foo'. It has the key 'y', which maps to 'bar'.

03:41 It has the key 'z', which maps to 'baz'.

03:48 And now we want to unpack that to use in f() as three separate arguments, so I would unpack it using the two asterisk syntax, and we can see that x, y, and z were given those appropriate values.

04:12 Next, we’ll summarize what we’ve seen so far about argument tuple packing and unpacking and dictionary packing and unpacking.

Become a Member to join the conversation.