Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Multiple Unpackings in a Function Call

00:00 Now let’s take a look at how we can unpack multiple items in a single function call. Starting with version 3.5, Python introduced support for unpacking multiple items, and we’re going to see that with this very simple function.

00:17 This function is going to take an arbitrary number of positional arguments and pack them in a tuple. I’ve put this in a file called multi_unpacking. The function g() we will see momentarily as it applies to dictionaries.

00:34 We’re just going to accept into the parameter args any number of positional arguments, which will be packed into a tuple.

00:45 So from multi_unpacking, let’s go and import both of them. So, f() is expecting an arbitrary number of positional arguments, but I have three collections with values I want to use in a single function called f(). I have a list containing the numbers 1, 2, and 3, I have a tuple containing the numbers 4, 5, and 6, and I have a set containing the numbers 7, 8, and 9.

01:20 So if I want f() to apply to all of these, I’ll need to unpack them. I can do that by specifying each one, unpacked, separated by commas. And so these will all be unpacked, which will eventually send nine different argument values to f(), which will then pack them all into a tuple.

01:45 And this particular function is simply going to display each value one at a time. And so there we see all of our arguments. And remember, there is no order to a set, and so the order in which Python internally represents the set and the numbers in the set might not match the order that they were put in, so don’t be bothered that the set displayed as 8, 9, and 7.

02:13 That’s the typical behavior of a set.

02:19 The same thing can be done with dictionaries. If I have a function, which I’ve called g() here, which is going to pack any number of keyword arguments into a single dictionary, I can, should I have a couple of dictionaries defined—let’s say that d1 is equal to the dictionary that maps 'a' to 1, maps 'b' to 2, and I have a second dictionary that maps 'x' to 3 and 'y' to 4, and I want to apply that to g(). g() is expecting any number of keyword arguments.

03:10 I can unpack the first dictionary, and then separated with a comma, I can then tell it to unpack the second dictionary. And what g() will see are going to be four key-value pairs. Now, let’s remember what g() is going to do.

03:30 g() is going to collect all of the keyword arguments into a dictionary, and then for each item, we’re going to find the key and the value, and then just display the mapping, the keyword to the argument value.

03:46 And so we should expect to see all four of those mappings because g() was presented them as four keyword arguments.

03:58 And you don’t have to have variables created. You can do unpackings with literals. So you can say, for example, call f(), and f() is expecting any number of positional arguments. So I can have a list and then another list.

04:20 I specify that I’m going to unpack each of those, and f() simply sees six positional arguments. And if I want to provide a couple dictionaries to g(), I can do them as literals.

04:40 So there’s one dictionary, and then I can unpack another dictionary.

04:50 So again, the two dictionaries will be unpacked, passed as key-value pairs to represent keyword arguments and their appropriate values. And then g() sees them, packs them all into a single dictionary, where we can see the key-value pairs that it was given.

05:09 Next up are a few lessons on how you can specify that some arguments must be given via keyword and how you can specify that some arguments must be provided positionally.

Become a Member to join the conversation.