 Welcome back everyone. Today we're going to be talking about dictionaries in Python. And dictionaries in Python basically are very similar to lists except instead of just having a value at a particular index, they also have a key. So you get this key value pair. Then you can do lookups in the dictionary by the keys name. So just to kind of do a quick overview or summary of lists. When we were talking about lists, we had a structure like this where we had the variable name, the list variable name equals, and then we had these square brackets. And then here we have three strings inside of this list, followed by a final square bracket. If I print list, which is our variable name, and then square bracket two, this two is the index, this two is the index that refers to one of these values. And if you remember, lists start with a zero index, so zero would be a, one would be B, and two would be C. So just looking at this list, if we do, if we run the program, then we get printing out C, because we're printing the second index in the list, okay? Well, that's how lists work, which they're a fairly useful data structure, but sometimes it makes more sense, or sometimes it's more useful instead of just saying two. Maybe I want to know, use something like name instead of two. So every time I'm looking at a list, or at a dictionary, I can say, give me the value of name inside this dictionary or inside this list. And then it will return the value associated with the key name. So let's go into that and see how that works. So first we have to define some dictionary. And we define it very similar to the way we would define a list. And I'm going to call my dictionary person. And we're going to have some attributes of a person, like name, occupation, age, things like that. So I'm going to say person equals. And that tells me that I'm setting up a variable. This is the variable that I'm creating. I know I'm creating it because I have this equal sign creating or modifying it, because I have this equal sign to the right of the variable. Okay, the next instead of square brackets, I'm going to use the curly bracket. So I'm basically saying here, I want a person variable. And then these curly brackets signify that I'm going to create a dictionary. Okay, so whenever you start a bracket, make sure you always end your bracket. So it's good practice whenever you just create your first bracket, always create the second one immediately, that way you can always know that it's there. Yeah, just make sure you keep track of your brackets, that will be a big, big source of your bugs while you go through your programming adventure. Okay, so I have person and then I've set up my dictionary. Now let's give it some key value pairs. So one key that I probably want a person to have is a name. Okay, so we do name. And then I want my value and I'm going to call this person Mary. And then I'm going to have another key value pair underneath that. Or as long as this I'm not just going to have one value in there. So I'm going to do a comma. And then I can give it another key value pair. So I'm going to do occupation. And let's say that Mary is a developer. Okay, and then let's do age and 24. So you can see here I have this key value pair. Name is the key occupation is the key age is the key. And then our values are Mary developer and 24. Notice what we've been talking about actually for quite a few videos now I have these strings. And I can tell that this is a string because it's quoted, right? So Mary is in quotes. So that means tells me it's a string. Developer is also in double quotes that tells me it's a string. Age is not in double quotes. So what does that tell me it is an integer, right? If we put quotes around it, then Python would treat it like a string. And then I wouldn't be able to do math directly on that that integer value. Instead, we're treating it as an integer. So if I want to add or subtract something from 24, I can treat it like a normal, a normal number, let's say. And then because this is the last value, there is no comma afterwards. So as if there's nothing after that, just leave it blank. So you do need to think about where your commas go. You have to separate these name value pairs. And then if there's anything after it put a comma, if it's the last one, do not put a comma at the end. And then of course, make sure you finish your dictionary with the the second curly bracket there. Okay, so now we've created our dictionary. That's pretty much it. It's just a list of objects with key value pairs, a list of objects with key value pairs, very similar to a list. Except now we actually have names for the value instead of the indexes. So instead of saying show me person at index two, I can just say show me person name. Okay, so it's much more convenient, I guess for a human to write. Okay, so now let's go ahead and just test this a little bit. And let's print out person just so you can see what the what the dictionary actually looks like. So we're printing, and we're just printing the entire person here, we're not printing any of the values inside. We would print everything. So here you go, we have age 24, name, Mary, occupation developer. Now I can refer to any of these keys, and then get the value out of that. So I can do a lookup for any of those keys. So for example, I can do print, I can do print, and then do person. And just like we did before with our list, I can do the square bracket, and then let's do name. So I want the person's name. Person, give me the key name and give me the value from that. So what should print out here, because I'm looking for the name of the person, I should get out, Mary. Okay, so let's save that, and run it, and we get Mary. Okay. So that's how we retrieve any of those values. I could also do person, for example, age, and then we should get 24. Okay. All right. So next, let's say that we have this age, and we actually want to change the value of age, because maybe Mary had a birthday, so now she's 25, and we need to update our person age. How could we do that? Well, kind of as you would expect, let me go up here. We access the variable, we can access the variable directly by the key, or by the key. So if we do person, and square bracket, and age, just like we printed out, except this time we're putting an equal sign on the right hand side of that, this becomes our variable, right? So inside the person dictionary, we find the key, age, and then we assign the value of whatever is to the right of the equal sign here. Okay. So let's say that she had a birthday, so now she's 25. We could also do, since this is a integer, we could add, just add one to 24 on the right hand side here, and we would get 25 installed here. So then that way, it would automatically update to the correct age, you wouldn't have to manually enter that. Okay. But just pay attention to the way we're doing the variable assignment, we have our person dictionary, we find the age key, and then we assign the age key the value of 25 instead of 24. Okay, so let's save that, run it again, and we get 25 because we're printing out the person's age. So just prove to you, just to prove to you that it's actually working. Let's print the person's age, then update the age, and then print it again. Okay. So 24, and then we updated the value, now it's 25. Okay. So let's say that we want to know all of the different keys that are in here. Let's do, we can iterate, we've already talked about four loops before, so we can iterate over the dictionary, just like we can iterate over lists, or that means loop over all of the values in the dictionary or values in the list. So we can do something like four, I'm going to say x in person. So for something inside person, and then we want to do something. So what do we want to do? Let's do print x. Now think about what's going on here. I actually let it slip a little bit ago. But let's see if you can figure out what we're talking about here. We have this person dictionary defined. We have a key value, we have three key value pairs. And then here I'm saying four x in person, print x. Okay. So what do you think is actually going to print? Is it going to print everything? It's going to print all of the key and value. Is it only going to print the key? Or is it only going to print the value? So in this loop, what will it print? And how many times will it actually loop over? So one way we can think about this is there's three values inside here, three keys, three values inside here. So most likely this is going to loop over three times because it's four x in person. So it's going to loop through each one of these lines. The only trick is which data is it going to return back to you, the key or the value? Well, in this case, I'm just saying give me x in person. And the first thing that this dictionary will look at is always the key. Right. So what should happen is four x in person should print, one of our printing this, it will give us the name, occupation, and age. Okay. It won't give us the value, it will only return the key. Okay, so let's take a look at that. Yeah, age, name, occupation. So these are the key names that are inside this dictionary. Now what we can do is use this loop to find out what the actual key names are, and then use those key names to pull up different values. So for example, I can add or remove key value pairs from a dictionary. So if there's a dictionary that I'm given, I might not actually know all of the different key value pairs inside that dictionary. So I might want to look up and then find specific keys, if specific keys exist, and then what their values are associated with that. So if we just do four x in person, print x, it's going to give us back all of the key names. Okay. Well, what if we want the value names instead? Well, we only have to change one thing. Person is already assigned as a dictionary. So we can do person dot values. Okay. So for x in person dot values, print x. Now x is being assigned on our first loop, we're going through, we come to our first line, but we're looking only for the values. So x becomes merry. Then we loop again, we come to the second line, we're looking for the values. So x becomes developer. And then we loop again, x becomes 24. So that's what we predict will happen. So let's go ahead and run this and see if that's it. Yeah. So 24, merry developer. Okay. So that's how we loop over the person values. So we got, we're able now from a dictionary to get all of the keys, we're able to get all of the values individually. Okay. And we're able to look up a value by key. Okay. So let's say instead of looping through everything, sometimes you just want to know, for example, if age, if a person has an age assigned or if they have a key value pair assigned, well, in Python, we can look for that directly. So we can just say if age, I'm going to look for the age key value pair, if age in person, and then we can print something if that's true. So if age is in person, this will evaluate to true. Now age is a key inside this dictionary. So this will evaluate to true. Then we can do something like print person has age. Okay. So if we run that person has age. Okay. Well, let's try something that's actually not in the in the dictionary. What can we say taco, if person, if taco in person, well, if you just had a taco, then maybe you have a taco, but let's say that the taco key value is not in the dictionary. So this actually evaluates to false. So age exists. So this will evaluate to true. And then we will print person has age. If taco is in person that evaluates to false because we don't have a taco value, then we can print person has taco if that's true. But we know that it's not true. Okay. So if we run this again, what should happen? Only one line should print and it says person has age. Everything else is false. Okay, person has age. Now if we change this and if we say if not taco in person, well, now we've switched that around. So taco in person is false. But I'm saying not. So if not taco in person, then a false that is not get switched to a true and then we have person has taco. Okay. So a little bit of logic to think about. We have this value that is negative. It's false taco in person. That's false. But I'm saying not. So not false is true. Okay, that's Boolean for you. Okay, just with these if statements just get used to evaluating if something is true or false. And then sometimes we need to use things like not to negate something. So that's doesn't have anything to do with dictionaries, but it does have to do with if statements and Boolean logic. Okay. Okay, so a couple more things that we can do actually probably should have started with this since we've already talked about it. We can also evaluate the length of the dictionary. You just by using Lynn person, like we did with lists, we can do the same with a dictionary. So see, see what the length of this is, and it evaluates to three, because we only have three key value pairs. Notice whenever you are looking at the length, it is not counting up the key and the value key and value key and value. That doesn't return six, it only returns three, because we have three key value pairs. Okay. Of course, we can also add values or new key value pairs to the dictionary. And this is what makes it so useful. We have person. And then if we want to add something, let's say that this person where they're going to a conference and we want to give them a shirt. So we can do shirt size equals. So if you look what we're doing here, we are setting up the dictionary, we have the square brackets again, and then we have a new key name that doesn't already exist inside the dictionary. So we have a new key name. If this key name already existed in the dictionary, since we're doing equals on the right hand side, we would be replacing the value. But since this new key name does not exist already, it will just be added as a new value. As a new key value entry. So we have the dictionary. We have shirt size as the new key that we want to insert into the dictionary. And then we need to give it some sort of values. Let's give it a value of medium. So Mary has a shirt size that is medium. So if let's run that, and then let's just print person again, so we can see the entire dictionary value. So here we have the new value or no new key value pair that we entered shirt size medium, we still have age, we still have name, we still have occupation. Okay, so let's say that's fine, we have a new key value pair entered. Well, what if we want to remove something? Imagine that maybe because of privacy reasons, we cannot keep the age value anymore. Just like before, we have our person value, sorry, our person dictionary, and we have our age key. And we want to actually remove this, not just the value, but we also want to remove the key name. Well, we can just put right before that delete or Dell. So Dell person age, and then that will remove this entire line. Okay, so notice, last time we ran the program, we inserted inserted shirt size, but we still had this age 24 in here. So now let's run this again, we're inserting the shirt size again. And then we're removing the age key value pair from the dictionary. So here we have it shirt size, name, occupation. So no more age in here because we have deleted the age key value pair. Of course, if you wanted to just delete the value inside the key value pair, you could do age and then just give it, for example, an empty string, and then that would override it with the empty string value. So we still have our age key, but we have an empty string for the value, right? But if you actually want to delete it, then you can just remove it completely with the Dell command. Okay, so Dell and yeah, that's pretty much it. So we've successfully created a dictionary. And a dictionary gives us these key value pairs. And then that lets us refer to different points or different data by name, rather than by index. So here we can say what is the person's name. And then it would return back Mary. So these key value pairs in the dictionary are very handy for keeping a lot of different information about more complicated data structures than lists. So in Python dictionaries are used quite often. So it's good to get used to how to deal with them. Once you create the dictionary, you can, of course, add new key names with their value pair. And you can also delete key value pairs from the dictionary. Okay, so that's it for today. Thank you very much.