>> Hello everyone and welcome to intro to
Python here on Cybrary on demand.
I am your instructor Joe Perry
and you are watching Lesson 9,
the final lesson of this class in which we are going to
be doing a deep dive on Python dictionaries.
Just like with the last lesson,
we only really have two objectives here.
We're going to learn the key dictionary methods,
and we're going to learn to make use of dictionaries.
We talked about dictionaries very briefly in Module 1.
We haven't really dug into
them the way they deserve to be dug into
because dictionaries are incredible.
You can do so much with them.
They're just these ridiculously useful tools.
We're going to really dig into those.
We're going to spend some time
on them here in this lesson.
First, just like we did with lists,
we're going to create our first dictionary
which we're going to call dict_one.
Actually we could do underscore what the word one.
We're just going to
assign a couple of simple key-value pairs.
Remember that as we do with lists,
we can address inside of dictionaries.
But instead of using indexes, we use the key.
Note that you can't reverse that.
You can't use the value to find the key,
it has to be the key to find the value.
Anytime you give a dictionary a bad key,
it'll spit out a key error and it's just going to
tell you whatever you used was not valid.
Just like we did with lists and strings and numbers,
let's do a dir against
that dictionary and see what things are available.
Now, it's a shorter list than it were for
strings because you can do crazy amount with strings.
But here you see we've got clear copy
from keys, get items,
keys pop, pop items, set,
default, update and values.
The ones that I really want to look at, first of all,
let's look at dict_one.keys.
Here you can see this is a built-in method.
That means that it's a function.
We can address it. You see it's going to
spit out this list right here of 1, 3, and 5.
Dict keys is basically just,
I'm not going to dig too far
into exactly what that means,
but essentially it's an iterable,
it's something that you can address.
For example, you could do something like for i in
dict_one.keys, print i.
What this is going to do is it's going to get
back essentially list of keys.
It's going to go through key by
key printing the values associated.
Here you can see that we print 2, 4,and 6.
That's one of the really useful things you can do
with dictionaries is get all of the keys
back and print all of the values if we wanted to.
This is a slightly advanced use of for loops,
but I'll show you for k, v in dict_one.
keys. Actually I'm going to do that
>> slightly differently.
>> For k in dict_one.
keys. [NOISE] Print,
format, k, dict_one.
That's a slightly complex
function that we just wrote there.
What we're doing is we have these two.
Remember our brackets from our discussion of strings.
We're using Format,
and we're saying the first item to insert is the key.
The second item to insert is the value that we
find by looking it up in our dictionary.
[NOISE] You see here that it will print 1,
2, 3, 4, 5, 6.
That's an easy way to find all of
the associated keys and values in a dictionary,
Now let's look back at our list
of functions for dict_one.
We see that we talked about keys.
We also have pop here,
and pop is a little bit different in
a dictionary than it is in a list.
To see that we're going to
go ahead and we're going to do help.
[NOISE] You see that
you have the argument here of k
and an optional argument
of d we're not going to address right now.
Then it's going to return v. It
removes the specified key
the same way we did with pop and lists,
and instead of giving you back that key,
it gives you back the corresponding value.
I'll show you, [NOISE]
and we'll give it the key of five.
It printed six. Now if we look at the dictionary itself,
we see that it no longer contains that key value pair.
That's a little bit different because you're actually
popping with a specific argument
telling it which key to take.
It's not just taking the last item,
it's taking a specific key,
and it's removing that key and the value from
your dictionary and it's printing
or returning back to that value.
That's the general concept to dictionaries.
The uses that you're going to get out of these,
there's so many potential use cases for dictionaries.
You can associate data with them.
You can get access to all of the functions,
all the methodologies inside of them.
In our lab, which is going to be
coming up right after the summary and review video,
you get a chance to really play around with
dictionaries and see all the different uses of them.
But I wanted to show you just again how to create them,
how to address them, how to find the methods of them.
They are just these remarkable
tools that I absolutely love.
I get really excited talking about them.
In this video, like I said, we talked
about the key dictionary methods,
and then we discussed the different uses of dictionaries.
In your supplemental materials
you'll get to do a lot more of that.
If you've finished this video,
you're about ready to do our summary and review and
then our final lab and our final assessment.
I just want to thank you for
being here with me through all of this,
and I'll get a little bit sappy
about it in our next video,
but just thank you so very much for watching this.
I is always have been your instructor,
Joe Perry, and you've been watching,
you've completed your last lesson of intro
to Python here on Cybrary on demand.