Hi, everyone. Welcome to this week's programming tip.
In this tip, we'll simply
look at more methods for working with dictionaries, in particular,
we'll look at methods that allow us to
iterate over the keys and the values of a dictionary,
and we'll write some simple code that then basically
echoes the contents of the dictionary to the console.
Let's get to it. Okay, let's look at
three important methods that we can use to manipulate the contents of a dictionary.
So, I have a sample dictionary here.
It's just basically last name, first name,
so you give it a last name,
it gives you back the corresponding first name.
And, I've got a little function here that's going to essentially go through and use
three important methods that we have at our disposal for working with dictionary.
The first method is the keys method,
and this returns back an object that contains all the keys in the dictionary.
The second method is the values method.
It returns back an object that contains all the values in the dictionary.
And finally, the items method returns back
all the key value pairs that exist in the dictionary.
Now, an important thing about this,
as I said, returns back an object.
When you actually run this code,
you're going to see, it doesn't return back a list.
In Python Two, these methods return the list.
In Python Three, these methods return
iterator that iterates over the contents of the list.
If you would like to get an actual list,
you can simply use the list function here to
actually convert this iterator into a traditional list.
So let's run this, and I'll say a little bit about why Python Three takes this approach.
So here's what we see if we run this.
The first three prints print back something that says dict_keys,
and well, here all the keys.
Dict_values, here are all the values.
Dict_items, here are all the items.
And notice, this is what we expect inside here,
but this is wrapped by these things: dict_keys, dict_values and dict_items.
Notice each of these iterator objects that Python Three is returning.
If you go down and you look at applying list of these,
you can see it just strips away
these wrappers and gives you the list that you normally expect.
Now, the whole purpose behind returning an iterator instead of a list is efficiency.
Imagine we had a dictionary with a million key value pairs.
If we'd like to iterate through all those key value pairs,
we don't want to automatically make a list with a million keys,
or a million values, or a million key value pairs.
And so, by using these new methods in Python Three,
you can achieve more efficient implementations of things that process dictionaries.
Now, for what you're doing with scripting efficiency maybe
isn't the primary thing right now,
so what you're going to see is, most of the time,
I'm just going to convert these into lists when we work with them if we need to see
the entire collection of keys, values or pairs.
Okay, let's use these methods and write
some simple code to display the contents of the dictionary in the console.
So here, I've implemented two different functions that we can
use to display the contents of the dictionary in the console.
So the first function, print_dict_keys,
is going to iterate over the keys in
the dictionary and help print out the contents of the dictionary kind of line by line.
So here's what I'm doing.
I'm printing out, using print,
the contents of the dictionary.
You normally see it.
Then what I'm going to do is I'm going to iterate over the keys,
and to do that I just simply can use a for loop say for key in my_dict,
I print out the key,
and then the corresponding value that I have looked up.
Most important thing to note here is when I say for key in my_dict,
by definition, by default,
this iteration iterates over the keys.
You could equivalently just replace my_dict by my_dict.keys here,
and that would also work.
Okay, the second method,
print_dict_items, is going to iterate over the items in the dictionary.
Remember, an item is a key value pair.
So what my_dict.items returns is a list
whose entries are items which are the form key, value.
And so here I'm going to say for (key, value) in my_dict.items.
And so, for each iteration,
we're going to get a key and a value.
And then I've gone through down here and I've
just actually simply printed it out as well,
each out on their own separate line.
So let's run this and see what happens.
And what you can see is exactly what we expect.
Here, we're putting out kind of a header line that shows what the dictionary looks like,
kind of standard Python form.
And then, we basically have kind of one line per key value pair in the dictionary,
and some little more readable than what you might see up here.
And below we see exactly the same output from print_dict_items,
exactly the same thing again.
So these are kind of two techniques
for working with the keys and the values in a dictionary.
So, have fun experimenting with them.
I think this will hopefully give you a little bit more understanding about
how dictionaries and iterating over the keys and values works. See you next time.