 Hey, welcome back to Python for Java coders. This is part eight where I talk about hash maps in Java But in Python they are called dictionaries now for my students who are watching this we didn't do hash maps because it's not part of the AP Plan, but they're quite useful and quite powerful. So let's take a look real quick here You can see that a hash map needs to be Imported okay, and a hash map is very much like an array. So an array you store some values by number there They're ordered. So item zero might be You know this you know item zero might be Bob item one might be Jennifer items to might be J. R. Etc. Etc. Etc. Etc. A hash map is stored by what's called a key and a value and If we take a look at the code here, you'll you'll see what we're doing It's probably make a lot more sense here in Python since it's something new to these to my students Hey, so what I'm going to do is I'm going to create a dictionary called capitals and In this case, I use curly braces If it was a list I would use square brackets, but here I use the curly braces and What this lets me do is Is something very very fascinating is I can do the following capitals Japan equals Tokyo Capitals China Equals Beijing capitals South Korea at least South Korea is So so this lets me store a great deal of data into one structure, okay, so Now in Java you can see over here. You have to of course declare it It's a hash map my key is a string my value that I'm returning is a string So just like you have a key here. It's a string the value here is a string of course Python being Python It's much simpler. You don't have to worry about all that kind of nonsense over here It's a new hash map again string comma string And then here what I've done is I've declared country as a string and capital as a string again Python You don't need to do that in Java. I had to use put so into the capitals hash map I put the key Japan which is associated with Tokyo China and Beijing and South Korea was Seoul So if I actually here if I did when I hadn't said print capitals capitals And if you run that then you can see I've got Japan is associated with Tokyo China is associated with Beijing and South Korea is associated with Seoul Now in Java you need to use the get method to get the value out of the key But of course Python being Python is much easier capital equals capitals There's put Japan and then print Japan colon and format Capital let's go ahead and run that and see what it looks like capitals. Of course. I spoke capitals wrong somewhere capitals spelling errors Happens everybody so Japan. Oops. I forgot my It's really brackets there. Okay, so Japan and Tokyo Yeah, I can go ahead and do the same thing here So I could do print. I'm gonna say country Equals South Korea and I'll say capital equals capitals Country, so I'm just using variables. This is nothing. There should be nothing new and then again print Boom-boom space boom-boom format Country and the capital so if I run this So you see South Korea is so now of course I could have done this I could have just put South Korea here And I could have put Actually capitals I can do that you can always substitute it just such as like you can in other circumstances But again, if you notice here, I'm keeping my variable names very clear so This is a dictionary of capitals. The key is the country and the value Is the capital? Okay, so this refers to this Let's say that's how I do it at least it makes it makes sense. I think to my way to do it that way Here's something if I put Korea instead Let's see what happens We get an error Okay, it's called a key error because that doesn't exist in the list So if I try to access something that doesn't exist in the list, I'm going to get an error So you have to be very careful with that One thing you can do actually I'll just show this it's not over here, but if I what I could do is like this If country in capitals So that'll tell me is it in there or not if it's in there go ahead and print it otherwise I'm gonna put something like else print sorry That not exist Or that key doesn't exist how you want to put it So that country doesn't exist because it's not in the list. So that's a way to avoid that particular error Okay Yeah, so let's move on to the next one and this is how we would print out the entire Dictionary, okay, so this is the for each method. So in the case of a Dictionary so we say for country because that's what the keys are in capitals the capital equals Capitals Country the current country and then print See here Format country Let's go ahead and run that space here print Run that and we should see South Korea Seoul Japan Tokyo China Beijing and notice the order is a little bit different here the way that Java orders things is different to the way that Python orders things and there have been some changes in Python recently The thing with hash maps is they are not ordered. They're not guaranteed to be in a certain order lists and sorry Arrays and array lists are always in the same order because you can choose they are ordered lists hash maps are a type of Unordered list so it could come out in any order when you do it like this now So you can see I did it for country and capitals now It might be easier if you think of it for key and capitals and value equals And we could put key and value So this is a more general format So for key and dictionary name the value is the dictionary name and the key I'm gonna put key there and then we print out the key and value But if you know what type of data is in there again, it might make sense to say country and Capital so let's go run that just make sure we get the exact same output. So these are again a very very useful Structure to use you can also do something like this I'll just go ahead. I'll put capitals I can say I'm gonna make a new a new list and we get rid of the old one and I'll say USA and Washington DC comma So it's a key and value pair comma key value pair comma key value pair And you always put a colon in between them the value. This does not have to be a string I could have put the population That's right. So I could say I might say population USA 2000 I could put Japan Japan's about 120 million give or take Etc. Etc. I could the key again doesn't have to be a String it could have been I think I even do numbers. I haven't tried it for a while But basically it's very very flexible as opposed to of course arrays and array lists over here in the Java world or hash maps I should say sorry the Java world where you have to declare it's a string and a string It's always gonna be a string and a string if you want to change that You have to make a new hash map with a different type System so Yeah, so that's dictionaries in Python coming from the hash map world of Java. So, yeah, hope that hope that was helpful Stay tuned. Thanks for watching