2.10 Dictionaries (Deep-Dive) - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

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