-: Let's talk about our next data structure,
and this one is called a dictionary.
In other languages it might be called a hashtable,
maybe map, or objects.
In Python, we have this idea of dictionary,
or as Python likes to call it, "dict."
I have to make sure I pronounce that properly,
that's a really hard word to say when you are recording
and you are repeating it over and over.
Anyway, dictionary. What is it?
Well, it is a data type in Python
but it's also a data structure.
Remember that I mentioned this idea of data structure
when we talked about lists.
They were the first data structure
in Python that we learned.
It's a way for us to organize our data
in a form that is, has some, different pros and cons
in how we access it.
For example, with lists we saw that they're easily ordered,
we can access them through indexes,
like 0, 1, we can reverse them, we can insert into them.
It was really, really nice.
What about dictionaries?
Well, let's see what a dictionary looks like.
A dictionary will look something like this:
we'll have a key,
and a value,
and then another comma, and then another key,
and another value.
Let's decipher this.
So I'm using curly brackets here
which denotes a dictionary
and unlike a list we have what we call a key value pair.
A key is a string for us to grab the value.
What do I mean?
Well, in order for us to access any of these values,
I would go dictionary and then say the key.
In our case lets say we want to grab "b."
We would grab the key
and then when we print here, the result,
we get the value two.
We're essentially telling the computer,
"Hey, I have this variable dictionary."
Remember I can name this whatever I want.
"I have this variable dictionary,
I want you to go find the key 'b'.
And this key 'b', if it exists grab me the value."
So, it's going to go into your memory,
or into the machines memory.
It's going to find where "b" is stored in our memory
and grab the value.
So in the bookshelf "b" we have the value 2.
What if we do "c" which doesn't exist?
If I click run , nope, I get an error.
It tells me "Hey, sorry, you're looking for key 'c' but,
I don't really have that key."
And this is what a dictionary is.
A dictionary is an unordered key value pair.
What do I mean by that?
Unordered means that they are not right next
to each other in memory.
Remember with lists, we can access lists with index of 0,
then index of 1, index of 2.
All those bookshelves were right next to each other.
A dictionary on the other hand, they're all over the place.
Although I've put "a" and "b" here,
this might be in one spot of memory,
and this one in another spot of memory.
And if I added, let's say "x" this would be
in another spot of memory.
And this might not be in order, maybe I've written it,
"a, b, x," but when I actually return the entire dictionary,
maybe, I don't have this in order.
These are all just scattered all across our memory.
As you see here, when I receive the dictionary,
I get these things in order,
but that's only because it's small.
If I had a really, really large dictionary,
I might not have them in order that I've inserted it in.
So, a dictionary is an unordered key value pair.
And as long as we know the key,
that is whatever the key that we are looking for,
then we just give that and our computer is going
to know, hey, where in memory to look to grab the values.
Now here's the cool thing about data structures,
like dictionaries and lists.
They're containers around data, right?
So, this can be anything that we want.
Let's say we want this one to be a list: 1, 2, 3.
Maybe we want this to be a string, 'hello.'
And maybe this is a Boolean value like, True.
That is completely valid.
I can access the "a" like so, and I get the list.
What if I want the second item in the list?
Well, I do index of 1, like this.
And there you go, I get 2.
And this is the same with list, and you'll see this a lot.
Where if we had a list,
or let's call it, "my_list"
and my list contains a dictionary
that has, let's say, "a, 1"
and you know what, let's just copy and paste this.
We'll do this.
So we'll have the first item in the array,
like this.
Then comma, then another item in the array,
and this time around this one has 4, 5, 6.
So that I can now grab from my list,
the first item in the array
and then maybe grab the "a,"
that is the "a" key.
So remember, this is a string.
And then we want to grab, let's say the third item,
so we'll go 2.
If I run this, I should get 3 and 2.
There you go.
Now, you might be asking yourself,
"Hm. What's the deal with these data structures?
Like, why do we need dictionary? Why do we need list?
Why can't we just have one thing that does everything?
Isn't that simpler?"
Think about that.
Because in the next video, I'm going to try
and answer that question for you.