Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's see how to provide arguments to functions with the keys and values in our dictionaries.
New Terms
Unpacking a dictionary - Pulling multiple keys and their values of out of a dictionary to feed them to a function.
>>> my_dict = {'name': 'Kenneth'}
>>> "Hi, my name is {name}!".format(**my_dict)
"Hi, my name is Kenneth!"
Packing a dictionary - Putting multiple keyword arguments into a single dictionary.
>>> def packing(**kwargs):
... print(len(kwargs))
>>> packing(name="Kenneth")
1
-
0:00
Sadly Python dictionaries can't help us with travel.
-
0:02
I know, I know I said packing and unpacking and you got all excited.
-
0:05
I know I did when I first learned about it.
-
0:07
If you haven't heard of this idea before though, it's all about expanding and
-
0:10
compressing variables.
-
0:12
When we talk about packing, we're taking multiple inputs like multiple values and
-
0:15
combining them into a single variable.
-
0:18
Each value is still represented in the final variable so
-
0:20
this isn't like adding two numbers together.
-
0:22
When I'm talking about unpacking though, as you can probably guess,
-
0:25
it's the opposite action unpacking takes the values in a variable and
-
0:29
makes a bunch of new variables from it.
-
0:31
Both of these actions are amazingly useful in coding, learn to use them well and
-
0:34
you'll be glad you did and
-
0:35
find yourself feeling a bit more powerful in your programming life.
-
0:38
That's going to work spaces and explore both of these concepts.
-
0:41
Most of the time, you'll encounter dictionary packing and
-
0:44
unpacking only in relationship to functions.
-
0:47
This is mainly because this is where it's the most useful.
-
0:50
But, just because it's most useful in a particular situation doesn't mean it's not
-
0:53
useful in other situations too.
-
0:56
So first, let's look at how we can pack a dictionary.
-
0:59
Might do this in a file called luggage.pi,
-
1:02
because you know you pack and unpack luggage.
-
1:06
Okay, so let's make a function named packer, and
-
1:10
it's going to take a new argument that you probably,
-
1:13
you might have seen this style before and you might not have.
-
1:15
But it's gonna take (**kwargs): or asterisk asterisk kwargs.
-
1:20
And all I'm gonna do is I'm gonna print out kwargs.
-
1:23
So, just print it out to see what's there.
-
1:26
And then down here, I want to call packer and
-
1:31
I'm gonna pass in some variables, some keyword arguments rather.
-
1:34
So the name is going to be equal to Kenneth.
-
1:37
The num gonna be equal to 42 and
-
1:40
spanish inquisition is gonna be equal to None and I'm wanna save that.
-
1:47
Now this might look a little weird to you probably haven't seen the double
-
1:50
asterisk used in programming outside of
-
1:53
maybe being used to create exponents like five to the power of two.
-
1:57
In Python,
-
1:57
when you use double asterisks like this on a parameter in a function Python will pack
-
2:03
whatever keyword arguments come into the function Into a dictionary.
-
2:07
Because you're packing keyword arguments, you'll see this name, kwargs, for
-
2:11
keyword arguments, quite often, right?
-
2:15
So let's run the script and let's see what it does.
-
2:18
So python luggage.py, and it prints out a dictionary.
-
2:23
num, 42, name, Kenneth, spanish_inquisition, None.
-
2:26
That's pretty much what we passed in here isn't it.
-
2:29
When the function prints out the kwarg's variable we can see that it's a dictionary
-
2:32
and each one of the keyword arguments that we gave to the function ends up as a key
-
2:36
in the dict and the value of the argument ends up as the value of the key in the dict.
-
2:41
So great It's fairly straightforward and it's very reliable but does this catch
-
2:46
all keyword arguments or could I have some that are explicitly defined and
-
2:50
then have a catch all for any others.
-
2:52
So let's say here that name is equal to none and then the rest are kwargs.
-
2:59
So let's go and run this again.
-
3:02
And I just have numb and Spanish inquisition, so now our name variable
-
3:06
isn't in the cards dictionary because it's explicitly caught outside of the packing.
-
3:11
So packing a dictionary takes a bunch of key value pairs and
-
3:14
turns them into a dictionary's keys and values.
-
3:17
So now let's see about unpacking.
-
3:20
I'm gonna start by making a new function.
-
3:24
And I'm gonna call this function unpacker.
-
3:27
And it's gonna take a first name which is equal to none, and
-
3:30
it's gonna take a last name which is equal to none.
-
3:34
And or gonna say if first name and last nam, so both of those things are there.
-
3:39
Then we're gonna print Hi something, something.
-
3:45
And we'll call .format with first name, last name.
-
3:51
And we need another closing parenthesis on that and
-
3:56
then else, we're going to print, hi no name, cuz we don't have a name.
-
4:01
So our function is looking for two keyword parameters, right?
-
4:05
First name and last name.
-
4:08
If it gets them both it'll print out a greeting with the names.
-
4:11
Otherwise, it says hi to someone named no name.
-
4:14
Now this isn't using packing or
-
4:16
unpacking I'll call it though with the power of unpacking.
-
4:20
Let's do this down here below packer.
-
4:22
We'll say unpacker, and I'm gonna do start star, and then a dictionary,
-
4:29
last name is gonna be Love, and first name, is gonna be Kenneth.
-
4:37
Close my parentheses.
-
4:39
Okay, let's run this again.
-
4:41
And now I got Hi Kenneth Love.
-
4:43
So, I've called the function and give it a single dictionary, right here,
-
4:47
one single dictionary as the only argument right?
-
4:50
And I didn't even name it, so it's not a keyword argument.
-
4:53
But because of these two asterisks that are here in front, python took each key
-
4:58
from the dict and its value and sent them as keyword arguments to the function.
-
5:03
Now you might be thinking but they're out of order, but
-
5:07
that's fine because keyword parameters on a function don't have to show up
-
5:11
in a particular order as a bit of a behind the scenes peek.
-
5:15
That's because they're actually a dictionary, when the function is processed
-
5:18
and dictionary keys don't have an order anyway.
-
5:21
One very important note about star star kwargs.
-
5:24
It has to be the last parameter in your function definition.
-
5:28
Python will tell you when you forget this rule though so
-
5:30
you don't really have to memorize it at least not right away.
-
5:34
Unpacking and packing dictionaries is one of the areas that often trips up
-
5:37
a lot of new Python programmers.
-
5:39
It's a handy but strange bit of Python, and
-
5:41
one that doesn't show up in a lot of the languages.
-
5:43
I heavily heavily suggest you practice as much as you can with these two approaches.
-
5:48
Dictionaries are one of the most powerful data types in python.
-
5:50
They can be used as impromptu objects, developed function arguments and
-
5:53
just as a good way to collect data like all of the other collection types in this
-
5:57
course they can be iterated over but it might not work the way that you expect.
You need to sign up for Treehouse in order to download course files.
Sign up