Let's talk about our next data structure, and this one is called a dictionary.
In other languages, it might be called the hash table, maybe map or objects.
In Python, we have this idea of dictionary or Python likes to call it DECT, have to make sure I pronounce
that properly.
That's a really hard word to say when you're recording and you're repeating it over and over anyway.
Dictionary, what is it?
Well, it's 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.
There were the first the 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 and how we
access it.
For example, with lists, we saw that they're easily ordered.
We can access them through indexes like zero one.
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.
We'll look something like this will have a key.
And a value.
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, let's say we want to grab we would grab the key and then when we
print here the result.
We get the value to we're essentially telling the computer, hey, I have this valuable dictionary,
remember, I can mean 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 machine's 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 to what if we do see which doesn't exist.
If I click run now I get an error.
It tells me, hey, sorry, you're looking for Kissi, but I don't really have that key.
And this is what a dictionary is.
A dictionary is an unordered key value pair.
What I mean by that unordered means that they are not right next to each other in memory.
Remember, with lists we could access lists with index of zero, an index of one, index of two, all
those bookshelves or 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, a memory in this one, in another spot
in memory.
And if I added, let's say X, this will be in another spot in 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 received 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're 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 their containers around
data, right?
So this can be anything that we want.
Let's say we want this one to be a list.
One, two, three.
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 while I do index of one like
this?
And there you go, I get to and this is the same with lists 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.
One.
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 four, five, six.
So that I can now grab from my list.
First item.
Indira.
And then maybe grab.
The A that is the akey, so remember, this is a string and that we want to grab, let's say, the third
item, so we'll go.
To fight run this, I should get three and two.
There you go.
Now, you might be asking yourself, hmm, what's the deal with these data structures?
Like, why do we need a dictionary?
Why do we need a list?
Why can 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.