8. Using dictionaries in python
Oct 10, 2023
Dictionaries are probably the most popular data storage variable type. They are not ordered and rely instead on a key & value system. You can store a value to a specific key using a dictionary. You can even make a dictionary of dictionaries as we do in this video example.
All right, we've covered a bunch of ways
that you can store sequential information in Python.
We talked about lists, sets, and tuples.
But probably the most powerful and one
of the most widely used things is the dictionary.
It's very similar to what we've seen before.
The key difference is that it has keys and values.
So it's not ordered.
It's not like it's indexed from 0, 1, and so forth.
Instead, instead of having indexes,
it has a key and a value.
So the word for this is an associative array or a hash
map in computer science lingo.
But this is essentially how it works.
You have your key over here.
And that maps over to a specific value.
And so as long as you have different keys,
they can map to different values.
And that's it.
That's the idea behind the dictionary.
So we write them with curly brackets.
And you have to make sure that you
have a key and a value separated by a colon.
So let's do a quick example.
So let's use an example from Rick and Morty.
We're going to make a dictionary all about Rick.
So we're going to call it the RickDict.
So this is going to be equal to, again, we use curly braces.
And now we have to have a key and a value.
So let's do something like first name.
Now, these need to be immutable.
The key must be immutable.
So I cannot do a list.
It has to be a string or some sort of scalar.
Strings or numbers are the most common.
But you could also do a tuple, something that's immutable
you could use here.
So that is our key.
And it needs to have a value associated with it.
So the value is going to be Rick.
That's his first name.
Now, we can have multiple keys and values.
You don't have to have just one.
Like, you could have a different one down here.
We could have key number two.
A different key, as long as it's different,
it could be some sort of value over here, and so forth.
So for describing people, or automobiles,
or lots of things like this, you can see how useful this is.
Because you can just keep on adding new keys
and having associated values with them.
So let's add a last name.
Now we're going to do something immutable.
Let's do a string as the key for this one.
And then the value is going to be his last name,
which is Sanchez.
Now let's add another one.
Let's do his catchphrase.
So this is the name.
This is the key, his catchphrase.
And the value would be whatever his catchphrase is.
So wubba lubba dub dub.
Then maybe we'll do one more.
We need a comma here.
We'll do his age.
So the key there is age.
And it's going to be a value of 70.
So now we have created a dictionary for Rick.
Now if we open this thing up,
you can see that stored inside of it,
instead of having the index going from index 0, 1, 2, 3,
like that, now we have keys, very specific things.
In this case, these are all strings,
but we could have done other things.
These could have been numbers.
And then each one of these keys is associated with a value.
One is an integer, and these other three are all strings.
So we could take this exact same thing,
and we could make different dictionaries
to represent some of the different characters
in the show, right?
So we could do one for Morty, right?
His first name is Morty.
His last name is not Sanchez, Smith.
His, does he have a catchphrase?
I don't know if he has one.
This is a bad idea.
I don't know.
This is a bad idea.
And his age is 14, all right?
So let's make one more for his sister, Summer.
Okay, so we've got Rick, Morty, Summer.
Her first name is, of course, Summer, also Smith.
Her catchphrase is clearly Booyah.
And her age is 17 or 18.
Okay, so we can run these,
and now we have three different dictionaries, right?
So we are able to access items from these dictionaries
in a very similar way to what we've seen before.
The only catch is, when we were doing a list,
you would tell it which index
you wanted to pull the element from.
But these aren't lists,
because they're not ordered, right?
So instead, what we need to do is give it the key.
If we ask from the dictionary at a given key,
it will give us the value.
So let's do an example of that.
Let's come down here in our console,
and let's write, okay, Rick, Dick,
I want you to give me the value at.
Make sure to include the parentheses, right?
Let's give him first name, right?
Oops, first name.
All right, if we ask for that,
it will respond with Rick, right?
Straightforward enough.
What happens if you ask for something that's not in there?
So if we say Rick underscore Dick,
and let's say we ask for a key,
but you don't know what's in the dictionary ahead of time,
so maybe you assume that the word origin will be a key,
like the planet Earth or something.
And so you say, oh, great, tell me his origin.
If that's not in there, you get a key error.
And so if you want to avoid the key error,
you can do something like this.
Instead of doing Rick, Dick,
you can use an attribute of the dictionaries called get,
right?
So let's change these to curly brackets
instead of square brackets.
We're going to do that where we're asking for origin.
And then what's really great is if you run this,
and it's not there, it just won't return anything,
but it has an option for you to put a comma
and then give it a default value.
So we can say Earth, right?
As a default value, if it doesn't know,
it outputs Earth as a default value.
So that's one of the attributes, or the methods,
excuse me, that's one of the methods
for a dictionary that you can use.
What else can we say about these things?
Something cool you can do with dictionaries
is you can make a dictionary of dictionaries, right?
So let's do that one.
Let's call this like the messed up family dict, right?
So we're going to make a dictionary
out of their family members, right?
So to do this, we still have to have a key and a value.
So the key, let's call this the Rick dict, right?
So that's the key.
And when you ask for the Rick dict as a key,
the value you should get should be the Rick dict.
So we're just going to put Rick dict over there, right?
And then we can do the same thing for Morty and for Summer.
So let's go ahead and do that.
I'm just going to copy and paste a little bit.
So over here, instead of Rick dict,
now it needs to be Morty.
And this one here needs to be the Morty.
And then this one here needs to be Summer.
And this needs to be Summer.
Okay, if we run that now, we have created,
I misspelled that.
There we go, let's try that again.
Now we've created a new dictionary right here.
See this dictionary, inside it, it has three dictionaries.
So it's a nested dictionaries.
And you could click on any one of these,
like the Rick one has the same things that we had before.
So that is the idea of dictionaries
as a way to store data in Python.
So now that we've done that,
we're going to talk about operators.
And soon we're going to come back to these lists,
sets, tuples, and dictionaries.
And use them with for loops,
because we're going to want to iterate through these
and do things with them.
So that'll come in a future video.

