 Hello everyone. This video explores Python's dictionary type, and it is a follow-on video to our other videos on hashing and hash tables, which are used within Python's dictionary. This is actually what you're about to see anyway, is actually an excerpt from a video I recorded before. So I just wanted to give you this new introduction before I turn it over to my younger self. So Python has a data type called a dictionary. You can read about it in your book, and I know that many of you have already encountered dictionaries and have used them, but some people haven't. So we're going to start by using the dictionary and getting everyone familiar with it, because it is such an important data type in Python. It's immensely useful. Of course, it has its drawbacks too, and it's just not good at certain things. But like a list in Python, a dictionary is also a collection. So we can put a whole bunch of items in there. But we put them in and we get them back out in a very different way than you would from a list. So instead of appending and adding to a dictionary, you instead put, put being the keyword, a value that is associated with a key. So like an old-school dictionary where you look up a word and then the word has a definition, Python dictionaries you have keys and keys have values. To initialize a Python dictionary, and we're going to go play in some code here in a second, you initialize it by you define your variable and you initialize it with these curly braces or squiggly brackets or whatever you want to call them. It's the thing on your keyboard above the square brackets. And then you assign values to the dictionary collection by saying students sub, and then you give it a key value. So this key in a list is always an integer. In a dictionary, it can be anything. So here I'm saying students sub Bob, the string Bob gets the value 123. So this assigns the value 123 and associates it with the key Bob in students. So then later, if we request students sub Bob, we will get the value back 123. So dictionaries are an implementation of the map abstract data type and what a map does is it stores things in key value pairs. Let's go over to our interpreter and play around with this a little bit. So here I am in pie charm. And I'm going to make this bigger for you guys in a second. You can get to the Python interpreter by clicking this little button down here, which says Python console, and that'll pull up an interpreter like we used in 131 a lot. Or you can go up to tools and select Python console. Okay, so I'm going to open one of these first. Let me make this bigger. And now I'm going to open the Python console here. And there. So now I've got kind of the command line interpreter that maybe you're more used to with Python. So let's initialize our dictionary and play with it a little bit. So students gets and then the curly braces run that. Now there's no output from that, but you can see over here in Python, it has actually initialized a variable called students. And it's of type dict, right, dict for dictionary. And this little zero is telling me the dictionary is empty right now. Okay, so let's assign a value. Students sub Bob gets one. Actually, let's give them a different value. Let's say that what we want to do is store students Bob Smith. And when we look them up in our student list, what we want to get is their 850 number. So I'm going to give Bob a value. Okay, so nothing was returned out of this. But PyCharm is telling us, okay, in memory, the students dictionary, it has one value now. And that value is Bob Smith is the key colon and then the value associated with Bob Smith. So let's pretend that we were accessing the value Bob Smith. Okay, now what do you expect this to output? Well, when we call this, it will return the value associated with the key Bob Smith. Okay, so now we get back 850123456, which is just what we expected. Now we can overwrite this value. Let's give him a garbage number, not a real number here, right. And this is perfectly okay. And now when we call students Bob Smith, it gives us back his value. All right, so there's a collection here. And I can put a whole bunch of different things in here. And Perkins gets that students. Nope, is that how you spell her name? I'm not sure. Let's give her a different value. Right. So as I've done this, my students variable up here in pie charm, I'm growing. Right. It's got three people in it. Each has a different value in it. And I can look up. I spelled her name wrong. That's okay. Oh, important lesson. Right. So I have tried, I tried to look up and Perkins value, but I made a typo here. Right. So the dictionary is telling me is key error and Perkins does not exist inside the dictionary. So anytime you try and look up a value or excuse me, look up a key that's not in there. Say Joe, you're going to get back a key error. Now the keys do not all need to be the same type. I can put in students. I don't know what do we want to put in here? Let's just put in a float 3.14. It's happy to do it. Right. So in a Python dictionary, your keys and your values can be any type of object. It's totally fine. The Python dictionary does not care. And it's more than happy to kind of just do this for you. So here, now let's pause a minute and think about this. What's going on? So I've got a collection of items. The items are looked up by their key value. And then the value associated with that key is what is returned whenever you ask for a collection sub its key. Now unlike lists, right, you do not have any control over where in the list these things are being put. This is kind of pie charm is showing us kind of in the order that I inserted them. But what's happening under the hood is that Python's dictionary is using a hash table. And as we talked about in the previous videos, what determines where in the hash table something gets inserted depends entirely on the hash function. So we have no insight into how Python is storing these. Python is using a hash table as the data structure for its dictionary. But if you were to kind of look at the whole thing in memory, look at all the memory space allocated to the hash table. We don't know where Bob Smith is. We don't know where Ann Perkin is. We don't know where Leslie Nope are in that list in that block of memory. They could be anywhere. It's dictated by Python's hash function. We have no insight into that. But the benefit here though, right, is we've given up that control. But as we talked about, hash functions are just mathematical functions. And the computer is really good at that. So what the hash table is going to give us is essentially a big O of one search time that tells us is a person in the dictionary. And if so, here's their value. But if not, you get one of these key errors, right? So this is effectively big O of one search. So does a student named Joe Schmoe exist? No, that's going to be big O of one search time. And the big O of one search time means we could have 300 million, billion, trillion things in our hash table. It's going to be a constant time lookup. Okay, big O of one time. Now part of what we are giving up though, again, is control over where those things are in the data structure. We don't have any control over that. Python puts them, hashes them to where they need to go. The other thing is you kind of lose some of the power of iteration here, right? If you were intending to iterate over these things in the order they were inserted, or in the order of say some sort of natural ordering according to their keys, you can't do that by default. You got to do some other manipulation. But it is possible to iterate over the contents of a dictionary. You have to do it differently though. So say, let's re-initialize students. Let's blank it out, okay? There is a way to initialize a dictionary with some values. Okay, so let me try and figure out how to work this, okay? So I'm going to open my bracket. You can put things in a Python dictionary. You can initialize it with some values. So the way to do that is to first put the key. So let's get our people back in here. Let's put it, nope, in there. And we'll say that her student ID is like this. So I'm putting the key colon the value. Now I put a comma, okay? So that's initializing one key value pair in our dictionary. You'll notice up here, it's not saved yet, because I haven't closed this curly brace. But let's put some more things in there. And there's nothing that says that the values inside this need to be unique. So if we put Ann Perkins in here, we can give her the same value as another key. That's totally fine. Who else on the show? Ron Swanson, right? He'll be the last one. So the last one, I'm going to put him in, he does not get a colon after him. And then I'm going to close my swiggly brace. And here they are. They're all up in here. Now say I wanted to know, is Ron Swanson in my student list? Well, one way I can do it is I can say student sub Ron Swanson. Okay, yes, he's there. How about, I can never remember his name, Andy Dwyer, right? He's not in there. So I got a key error, which is throwing an exception. Exceptions are not great, right? If what you want to do is just check to see if a key is in there, if a person is in the dictionary, you should do it like this, Andy Dwyer in students, right? So here's the in keyword, kind of like you have with lists. And this will give you a true or false value, depending on if the key, not the value, if the key is in the collection, right? So Ron Swanson is in the students. But if you tried to search for his key value, it would not show up, right? Is that the right number of zeros? I think it is. One extra, right? So this is Ron's key value, but it's returning false, right? The in keyword searches the keys. Now, there is a way to get or to loop over all of the keys and the values inside a Python dictionary. You can do it like this. We'll use a for loop, for key in students.keys, okay? The students.keys will return us basically a list that we can loop over. It's not exactly a list, but it's going to give us each of the keys, and then we can do something with it. So we could print the key plus key comma is a great student, okay? So we can actually loop over the keys. We can also loop over the values like this. And then we can do something with each value if we wanted to, okay? So we do have ways of looping over the contents of a dictionary, but that's not really the dictionary's purpose, right? The cool thing about a dictionary is you can store a key and associate a value with it, and that looking up that key is big O of one time. But there are times when you're going to want to iterate through a dictionary. This is the way to do it. Well, suppose you need to iterate through both things at once. Here's a little syntactic sugar for you, right? For key comma value in students.items, okay? So what is student.items return? It's going to return effectively a list of tuples, okay? Where each tuple is a key value pair, right? So let's just see how this works. Print for key and student values. So key has the value value, okay? So this little loop and this little trick here is looping through every item in the dictionary, right? Every combination of key and value, and then printing them out, right? So it's just kind of a nice, neat thing that you can do with the Python dictionaries, right? So explore these dictionaries. Know how to use them. They are an incredibly useful data type.