- [Instructor] In this video, we're going to discuss
the dictionary method's keys and values.
And I'm also going to talk in a little bit more detail
about the items method as well.
Now for demonstration purposes,
we'll use this months dictionary,
which contains three key value pairs, once again,
and let's start out within a for loop here
in which we demonstrate the keys method.
Now, the items method that we looked at previously
gave us back each key value pair as a tuple,
the keys method simply grabs each key
from the dictionary and gives us back
something that's iterable that I can then
walk through and in this case I'll just
display the month name followed by a couple of spaces.
So let's go ahead and do that.
Now, as you can see here, the keys came out
in the same order that they were inserted
into the dictionary once again,
and that still is going to depend on
what version of Python you're using.
Now, if you installed Python three dot seven and higher,
as I specified back in the before you begin lesson,
then you'll wind up... you should wind up with
the same order that I'm seeing here,
however, if you're using an earlier version of Python,
the order in which these come out
may be different on your particular machine.
Now, let's also go ahead and demonstrate the values method.
As you would expect, it's going to give you back the values.
Notice that the values for the corresponding keys
came out in the same order as again,
they were defined in the original dictionary.
Now, I said when we first introduced
the items method, that it was returning
an iterator that allowed us to iterate through
in our case, the items previously,
and now, the keys and the values.
But in all three of these methods' cases,
what's really being returned is
what's known as a dictionary view.
A dictionary view is effectively a window
into the dictionary so you're looking at
that dictionary's data, and if you modify the dictionary
after you acquire the view, the next time you iterate
through the view, you'll see the modified contents.
So let me demonstrate what I'm talking about here.
Up above, we iterated immediately through
whatever keys returned, but let's say
we store that into a variable.
So now we have this object which is a view
into the months dictionary.
And let's iterate through that,
because we wanna just prove that we can.
So... whoops, sorry about that.
So if I go ahead and execute that,
you see that indeed, January, February, and March
are displayed as they were up above.
The only difference between snippet two
versus snippets four and five is that
I took the intermediate step of creating the
variable months view.
So now, if we go modify the months dictionary,
let's say we go and say months sub December
and we give it the value 12, now of course
the months dictionary has been modified
and we can see the updated dictionary
with four key value pairs.
So at this point of course, if we were to get
months dot keys or months dot values
and iterate through them like we did up above,
we would expect to see four items get displayed
in each case.
But what about when I iterate through
months underscore view, which was acquired
when the months dictionary only had three keys in it?
Well, let's see what happens.
So, for each of those keys, we're going to display them.
So for each key in the months view,
display the key followed by a couple of spaces.
And when I go ahead and do that,
notice that, indeed, we get four items being output,
even though we're iterating through the months view,
which was created back up here in snippet four
when we still only had three keys,
so this helps prove that in fact
the view is looking at the original dictionary's data,
it doesn't maintain its own copy of that data.
Okay, so now that we've seen the keys, values
and items methods and talked a little bit
about dictionary views, sometimes you'll want to
acquire either the keys, the values, or the full
item tuples to place them into a list for example.
So in Python it's real simple to do things like that,
I have the built-in list function
and I can say months dot keys
and it will simply walk through those keys
and insert them into the list
and return the new list object.
I can of course easily do the same thing with values,
and if you're curious, we can also do
the same thing with items in which case,
we see the actual tuple objects
that have been placed into the list
that was being created in snippet 11 here.
So this just further proves that the items method
really does give us access to tuples of key value pairs.
So at this point, let's just take a look
at one other thing that you may find handy
once in a while.
Sometimes it's useful to iterate
through a dictionary's keys in sorted order.
So in the case of our keys, these are strings,
so sorted order would basically mean
listing these alphabetically,
remember, however, that when you compare strings in Python,
it's based on the underlying numeric values
of the characters, so it's really a
lexicographical comparison.
Now in this case, each of the four keys
starts with a capital first letter,
so based on that, we would expect
that these will be sorted alphabetically
if we iterate through them in that order.
So let me go ahead and copy one additional
loop here and paste it in.
In this case, we're going to take the result
of the keys method and hand it off
to that built-in function sorted
that we demonstrated with lists in the preceding lesson.
So it's going to acquire the keys,
give me back an iterator that walks
through those keys in sorted order,
whatever sorted order means for the keys,
in this case lexicographical order
because they're strings, and for each of those month names,
we want to display them and in fact,
we get alphabetically now December,
February, January, and March.
Now you may not want to walk through month names
in that order, but you might be, for example,
keeping track of word counts in a body of text
and what you might want to do is order the words
alphabetically so that you can then
see by word easily what each count was.
Or maybe you want to order them not alphabetically
but by the values of those keys,
which would be the counts of the keys,
and there's lots of things like that
that you'll encounter as you start
working with larger bodies of data stored in dictionaries.