Dictionaries wouldn't be very useful if the only things we could do are create them and print them. We actually want to be able to use them and find key value pairs within them. So in this video, we're going to learn how we can look up the value that's mapped to a particular key or even change the value that's mapped to a particular key. All right. Let's get started. So how do we look up keys in a dictionary to figure out what values they're mapped to. Well, first we actually need a dictionary. So here we have the cipher dictionary that you've seen in a previous video that map strings that are single characters to other strings that are also single characters, and we're mapping all the characters in the the word python here. Okay. And so if we print that out we'll be able to see that dictionary. But you can also see it defined right here in the program. Now how do we figure out what the string t is mapped to. Well, we use indexing kind of like we used with lists, however in a list, the index was always numerical. It was a numerical index that was the offset inside of the list. Here the index is the key itself. So when we have cipher ['t'] we're saying, what value is mapped to the key which is the string t. Similarly when we have cipher ['n'] we're saying what value is mapped to the string n in this dictionary. Right, so let's see what happens. All right. As we can see, when we printed cipher sub t, it printed out n and when we printed out cipher sub n, it printed out p. If we look back up here at the dictionary, the string t is in fact mapped to the string n the string n is in fact mapped to the string p. Now let's actually use our cipher dictionary to do something interesting. You may be wondering why I called it cipher. Well, it's because I'm going to use it as cipher-text in order to encrypt strings. This is going to allow me to take characters out of a string and convert them to different characters, and then I will have an encrypted string that nobody will know exactly what it is. Right. So let's take a look here. I have a function that I call encrypt and you can see, it takes two arguments. The first one is a cipher dictionary and the second one is a word. And then when I look at what's going on in the function, I start out with an empty string which is the encrypted string that I'm going to return ultimately. And then I iterate through all of the characters in the input word and for each one, I look that character up in the cipher dictionary and get its associated value. I add the resulting value to the encrypted string and finally end up with the entire word encrypted or basically remapped to the values from the cipher dictionary which I can then return. Right, let's run this and see what happens. All right. You can see that I started out with the string Python. All right. Interesting. And I get back the string ohntyp. Okay. Now I can send the string ohntyp to my friends and if they know how to decrypt it, they'll know I'm actually sending Python. You'll know the secret now though too right. Now what happens if you try to look up a key in the dictionary when there is no mapping for that particular key? Well, seems like there might be a problem. Let's find out. And yes it is. We get something called the key error and Python tells you what you tried to use is a key that it did not have mapping for in the dictionary. So, what do you do here? Well, one way to help you out here is to use the get method of dictionaries. If you're not sure if the key exists but you still want to try and look it up, you can call it get. So, here are three cases. First, I'm going to call get with the string t. I know that is in the dictionary. So let's see what happens. Then I'm going to call it with the string one which is not in the dictionary. So let's see what happens. And then finally I'm going to call with two arguments here. Let's just see what happens and I'll come back to that. I still have my key here. I have to comment that out. Okay, so when I called get on t, I got n back. Well, that's right. We know that t is mapped to n in the cipher dictionary. When I called get with one which is not a key in the cipher dictionary, I get back the Python value none basically indicating there is no mapping. Right. And maybe I didn't want none though or maybe I actually do have things explicitly mapped to none in the dictionary so that becomes confusing. I can provide a default value that I would like back. And that's what this final form of get does, where I ask it to get the value that is mapped to the key one. And if there is none, I would like z back, right, and then gives you an easy way to have a default value and you can see here, one is not in the dictionary, so I got z back. Now let's just quickly here say hey, well, what happens when I call get with a default value and it is in the dictionary. Let's just confirm. Okay. We did get back n, right. So either it gives you back the value that's actually mapped to the key or it gives you the default value back if there is no mapping. So I hope you're starting to appreciate the value of dictionaries. I have these mappings from keys to values and I can look up what is mapped to any particular key by using this indexing notation or by using the get method and then that will tell me what value is mapped to this key. Right. There are a lot of interesting and practical uses here one of which I just showed you right. We can encrypt a word but there's more. Dictionaries also allow you to update the key value mappings. Right. So we have our cipher dictionary that we've had so far. And I want to modify what the key p is mapped to. So again, I use the indexing notation. Things are very similar to lists here where to update a key value mapping or use the indexing notation on the left hand side of the equal sign with the square brackets where the index is the key that I want to update. And then on the right hand side of the equal sign is the new value that I want to associate with that key. All right, so I want to change the mapping of the key p from being mapped to o to be mapped to q and let's see what happens. Well, in fact it is exactly what happens right. Originally my dictionary maps p to o as you can see here. And then after I modify the key value mapping, p is now mapped to q. But wait, there's still more here. I can create a new key value mapping in exactly the same way using the same syntax. Right. So if the key that I'm trying to update is already in the dictionary, it will just change what it is mapped to. If it is not in the dictionary, a new key value mapping will be added to the dictionary. So let's run this. Okay. And here we can see that there is a new mapping from the key r to the values z now. So I have a new cipher dictionary. Let's run encrypt again and see what happens using my cipher dictionary that has been modified with updated mappings. And you can see originally, when I encrypted Python I got ohntyp. Now I get qhntip I guess. In this video we learned how to use dictionaries. You've seen how to look up a key in the dictionary to find what value is mapped to that particular key. You've also seen that you can update the value that's mapped to a particular key or even add a new key value mapping into the dictionary. This allows us to create things like our encryption function where we can actually use these dictionaries. Hopefully you also have a little bit of an appreciation for the design of Python when you see that we use exactly the same kinds of indexing syntax that we've already seen when we've looked at sequence types like this. The only difference is that the index now is a key into the dictionary rather than a number that indicates the offset within the list.