‫Let's talk about our next data structure.
‫And this one is called a dictionary.
‫In other languages.
‫It might be called a hash table, 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'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 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 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?
‫Well, look, something like this will 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, let's say we want to grab B, we would grab the key and then when
‫we print, hear the result.
‫We get the value, too.
‫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 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 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, then 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 in memory and this one in an other spot in memory.
‫And if I added, let's say X, this will be another spot in memory.
‫And this might not be in order.
‫Maybe I've written in 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'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 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 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.
‫The.
‫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.
‫If I run this, I should get three and two.
‫There you go.
‫Now, you might be asking yourself, what's the deal with these data structures?
‫Like, why do we need a 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.