Packing with Dictionaries4:23 with Ashley Boucher
Learn how to pack dictionaries.
Packing in Python is a way to condense a series of variable arguments into a single 0:00 dictionary. 0:04 Where each variable name becomes a key, and 0:05 each variable reference becomes the corresponding value. 0:07 It's a lot like packing with Python sequences. 0:11 But instead of passing multiple positional arguments and capturing them into a tuple 0:13 you're passing multiple variable arguments and capturing them into a dictionary. 0:18 So that's a lot of words. 0:23 Let's back up. 0:24 What's a variable argument? 0:25 Well, oftentimes when we call functions we pass positional arguments. 0:27 These are just comma separated values that are received by the function 0:31 in the order in which they're sent. 0:34 That's why they're called positional. 0:36 The key difference between variable arguments and positional arguments 0:38 is that, for variable arguments, the order that they're sent doesn't matter. 0:41 And unlike positional arguments, they're named. 0:46 So, take a look at my workspace. 0:48 You can see I have a simple function and then a call to that function. 0:50 I'm using standard positional arguments here. 0:54 Now, if this function call is not placed close to the function definition 0:57 in my code, it becomes much less obvious what these positional arguments are for. 1:01 This can make it difficult to get up to speed if the developer is 1:06 picking up this code base for the first time. 1:09 Using variable arguments, however, which are also referred to as keyword arguments 1:12 or named arguments, can add clarity and simplicity to your code. 1:16 So, to convert these positional arguments to variable arguments, 1:21 I simply make them look like variable assignments 1:24 The variable names I use here have to match the parameter names in 1:41 the function definition. 1:44 This is how Python matches arguments and parameters when the order doesn't matter. 1:48 So, just like when our code can call for passing an unknown or 1:53 arbitrary number of positional arguments to a function, it might also call for 1:57 passing an unknown or arbitrary number of variable arguments to a function. 2:02 In both cases, that's when we use packing. 2:06 In order to change our example here to a function that takes advantage 2:09 of packing will change the function definition so it looks like this. 2:13 We'll replace the individual parameters with a single parameter kwargs, 2:17 Which stands for keyword arguments. 2:24 Which of you recall is another word for variable arguments. 2:26 And then we'll precede that with two asterisks. 2:30 This differs just slightly from when we pack positional arguments into a tuple. 2:34 For that purpose, we use a single asterisk often followed by the parameter name args. 2:39 Now, I'm also gonna edit the body of the function to print out the content of 2:44 kwargs by using a for loop, and I'll use an fstring to make the formatting pretty. 2:48 I'm gonna use the .items method. 3:03 And then I'll print. 3:08 Okay, now I'll save and run. 3:22 Awesome, so by converting our function definition to use packing, 3:29 any amount of variable or keyword arguments can be passed to a function. 3:33 I can send or not send arguments as needed. 3:38 In this example, I can add an additional keyword argument and 3:42 our function will still be able to process it without difficulty. 3:45 This is why packing can be so useful. 3:48 It allows for more seamless flexibility, which means your code and 3:50 your app can grow with fewer problems. 3:53 So, let's try to add another variable argument. 3:56 Now, if we save and run this, 4:08 we should see the newly added second topic keyword argument. 4:09 There it is, awesome work. 4:15 Okay, let's move on to the final lesson in this course to learn about dictionary 4:17 unpacking. 4:21
You need to sign up for Treehouse in order to download course files.Sign up