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 as Python likes to call it dict 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 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 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 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 b. 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 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 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 No 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 on ordered 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 0 then index of one index of to 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 an other spot a 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 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 a 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 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 one two three maybe we want this to be a string Hello and maybe
this is a volume 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 one like this.
And there you go.
I get two 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 1 and you know what.
Let's just copy 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 in the array and then maybe grab the A.
That is the key.
So remember this is a string and then we want to grab let's say the third item.
So we'll go to fight run this.
I should get 3 and 2.
There you go.
Now you might be asking yourself what's the deal with these data structures like why do we need dictionary
why do we need 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.