WEBVTT

00:00.000 --> 00:02.260
>> Hello everyone and
welcome to intro to

00:02.260 --> 00:04.840
Python here on
Cybrary on demand.

00:04.840 --> 00:06.370
I am your instructor Joe Perry

00:06.370 --> 00:08.095
and you are watching Lesson 9,

00:08.095 --> 00:11.530
the final lesson of this class
in which we are going to

00:11.530 --> 00:14.890
be doing a deep dive on
Python dictionaries.

00:14.890 --> 00:16.360
Just like with the last lesson,

00:16.360 --> 00:18.025
we only really have
two objectives here.

00:18.025 --> 00:19.765
We're going to learn the
key dictionary methods,

00:19.765 --> 00:22.060
and we're going to learn to
make use of dictionaries.

00:22.060 --> 00:24.700
We talked about dictionaries
very briefly in Module 1.

00:24.700 --> 00:25.870
We haven't really dug into

00:25.870 --> 00:27.130
them the way they
deserve to be dug into

00:27.130 --> 00:29.980
because dictionaries
are incredible.

00:29.980 --> 00:31.585
You can do so much with them.

00:31.585 --> 00:33.760
They're just these
ridiculously useful tools.

00:33.760 --> 00:35.230
We're going to really
dig into those.

00:35.230 --> 00:36.070
We're going to spend some time

00:36.070 --> 00:37.345
on them here in this lesson.

00:37.345 --> 00:39.415
First, just like
we did with lists,

00:39.415 --> 00:40.810
we're going to create
our first dictionary

00:40.810 --> 00:42.645
which we're going
to call dict_one.

00:42.645 --> 00:45.080
Actually we could do
underscore what the word one.

00:45.080 --> 00:46.550
We're just going to

00:46.550 --> 00:50.610
assign a couple of
simple key-value pairs.

00:54.620 --> 00:57.320
Remember that as
we do with lists,

00:57.320 --> 00:59.450
we can address inside
of dictionaries.

00:59.450 --> 01:02.820
But instead of using
indexes, we use the key.

01:03.520 --> 01:07.350
Note that you can't
reverse that.

01:07.350 --> 01:09.390
You can't use the
value to find the key,

01:09.390 --> 01:10.640
it has to be the key
to find the value.

01:10.640 --> 01:12.650
Anytime you give a
dictionary a bad key,

01:12.650 --> 01:14.450
it'll spit out a key error
and it's just going to

01:14.450 --> 01:17.360
tell you whatever you
used was not valid.

01:18.670 --> 01:21.850
Just like we did with lists
and strings and numbers,

01:21.850 --> 01:23.060
let's do a dir against

01:23.060 --> 01:26.030
that dictionary and see
what things are available.

01:26.030 --> 01:27.620
Now, it's a shorter
list than it were for

01:27.620 --> 01:29.855
strings because you can do
crazy amount with strings.

01:29.855 --> 01:31.730
But here you see
we've got clear copy

01:31.730 --> 01:32.750
from keys, get items,

01:32.750 --> 01:34.160
keys pop, pop items, set,

01:34.160 --> 01:35.765
default, update and values.

01:35.765 --> 01:38.155
The ones that I really want
to look at, first of all,

01:38.155 --> 01:41.040
let's look at dict_one.keys.

01:41.040 --> 01:43.805
Here you can see this
is a built-in method.

01:43.805 --> 01:45.995
That means that it's a function.

01:45.995 --> 01:48.890
We can address it.
You see it's going to

01:48.890 --> 01:53.105
spit out this list right
here of 1, 3, and 5.

01:53.105 --> 01:55.580
Dict keys is basically just,

01:55.580 --> 01:56.930
I'm not going to dig too far

01:56.930 --> 01:57.890
into exactly what that means,

01:57.890 --> 01:59.810
but essentially
it's an iterable,

01:59.810 --> 02:01.070
it's something that
you can address.

02:01.070 --> 02:05.045
For example, you could do
something like for i in

02:05.045 --> 02:13.320
dict_one.keys, print i.

02:13.320 --> 02:14.910
What this is going to
do is it's going to get

02:14.910 --> 02:17.970
back essentially list of keys.

02:17.970 --> 02:19.290
It's going to go through key by

02:19.290 --> 02:22.815
key printing the
values associated.

02:22.815 --> 02:25.875
Here you can see that
we print 2, 4,and 6.

02:25.875 --> 02:27.650
That's one of the really
useful things you can do

02:27.650 --> 02:29.330
with dictionaries is
get all of the keys

02:29.330 --> 02:33.350
back and print all of the
values if we wanted to.

02:33.350 --> 02:35.420
This is a slightly
advanced use of for loops,

02:35.420 --> 02:40.220
but I'll show you for
k, v in dict_one.

02:40.220 --> 02:45.290
keys. Actually I'm
going to do that

02:45.290 --> 02:45.950
>> slightly differently.

02:45.950 --> 02:47.195
>> For k in dict_one.

02:47.195 --> 02:58.430
keys. [NOISE] Print,

02:58.430 --> 03:02.880
format, k, dict_one.

03:07.730 --> 03:10.130
That's a slightly complex

03:10.130 --> 03:11.390
function that we
just wrote there.

03:11.390 --> 03:12.640
What we're doing is
we have these two.

03:12.640 --> 03:14.795
Remember our brackets from
our discussion of strings.

03:14.795 --> 03:15.910
We're using Format,

03:15.910 --> 03:18.560
and we're saying the first
item to insert is the key.

03:18.560 --> 03:20.720
The second item to insert
is the value that we

03:20.720 --> 03:22.760
find by looking it up
in our dictionary.

03:22.760 --> 03:24.750
[NOISE] You see here
that it will print 1,

03:24.750 --> 03:26.070
2, 3, 4, 5, 6.

03:26.070 --> 03:27.200
That's an easy way
to find all of

03:27.200 --> 03:30.300
the associated keys and
values in a dictionary,

03:30.520 --> 03:32.945
Now let's look back at our list

03:32.945 --> 03:36.235
of functions for dict_one.

03:36.235 --> 03:38.515
We see that we
talked about keys.

03:38.515 --> 03:39.770
We also have pop here,

03:39.770 --> 03:41.510
and pop is a little
bit different in

03:41.510 --> 03:43.250
a dictionary than
it is in a list.

03:43.250 --> 03:44.330
To see that we're going to

03:44.330 --> 03:45.560
go ahead and we're
going to do help.

03:45.560 --> 03:50.735
[NOISE] You see that

03:50.735 --> 03:52.820
you have the argument here of k

03:52.820 --> 03:54.890
and an optional argument

03:54.890 --> 03:56.545
of d we're not going
to address right now.

03:56.545 --> 03:59.570
Then it's going to return v. It

03:59.570 --> 04:01.220
removes the specified key

04:01.220 --> 04:02.990
the same way we did
with pop and lists,

04:02.990 --> 04:04.970
and instead of giving
you back that key,

04:04.970 --> 04:08.405
it gives you back the
corresponding value.

04:08.405 --> 04:16.410
I'll show you, [NOISE]

04:16.410 --> 04:20.140
and we'll give it
the key of five.

04:20.840 --> 04:24.875
It printed six. Now if we look
at the dictionary itself,

04:24.875 --> 04:27.355
we see that it no longer
contains that key value pair.

04:27.355 --> 04:29.420
That's a little bit different
because you're actually

04:29.420 --> 04:31.400
popping with a specific argument

04:31.400 --> 04:32.540
telling it which key to take.

04:32.540 --> 04:34.175
It's not just taking
the last item,

04:34.175 --> 04:35.735
it's taking a specific key,

04:35.735 --> 04:38.600
and it's removing that
key and the value from

04:38.600 --> 04:40.010
your dictionary
and it's printing

04:40.010 --> 04:42.055
or returning back to that value.

04:42.055 --> 04:45.270
That's the general
concept to dictionaries.

04:45.270 --> 04:48.165
The uses that you're going
to get out of these,

04:48.165 --> 04:51.365
there's so many potential
use cases for dictionaries.

04:51.365 --> 04:52.880
You can associate
data with them.

04:52.880 --> 04:54.650
You can get access to
all of the functions,

04:54.650 --> 04:56.600
all the methodologies
inside of them.

04:56.600 --> 04:59.000
In our lab, which is going to be

04:59.000 --> 05:01.280
coming up right after the
summary and review video,

05:01.280 --> 05:02.750
you get a chance to
really play around with

05:02.750 --> 05:04.925
dictionaries and see all
the different uses of them.

05:04.925 --> 05:07.130
But I wanted to show you just
again how to create them,

05:07.130 --> 05:09.595
how to address them, how to
find the methods of them.

05:09.595 --> 05:11.300
They are just these remarkable

05:11.300 --> 05:12.470
tools that I absolutely love.

05:12.470 --> 05:14.515
I get really excited
talking about them.

05:14.515 --> 05:15.920
In this video, like
I said, we talked

05:15.920 --> 05:17.350
about the key
dictionary methods,

05:17.350 --> 05:20.209
and then we discussed the
different uses of dictionaries.

05:20.209 --> 05:21.740
In your supplemental materials

05:21.740 --> 05:23.980
you'll get to do a
lot more of that.

05:23.980 --> 05:26.240
If you've finished this video,

05:26.240 --> 05:28.100
you're about ready to do
our summary and review and

05:28.100 --> 05:30.080
then our final lab and
our final assessment.

05:30.080 --> 05:31.370
I just want to thank you for

05:31.370 --> 05:33.080
being here with me
through all of this,

05:33.080 --> 05:34.220
and I'll get a little bit sappy

05:34.220 --> 05:35.270
about it in our next video,

05:35.270 --> 05:37.340
but just thank you so very
much for watching this.

05:37.340 --> 05:39.230
I is always have been
your instructor,

05:39.230 --> 05:41.090
Joe Perry, and you've
been watching,

05:41.090 --> 05:44.450
you've completed your
last lesson of intro

05:44.450 --> 05:49.210
to Python here on
Cybrary on demand.

