 Let's have a look at dictionaries. Now dictionaries are collections with two elements So they build on what we had with arrays and tuples and they really have a value and This idea of an of an index gets expanded to this value of a key Now the best way to do that is let's just create a dictionary so the dictionary is actually a little function called DICT capital D there dict and functions always are encased in these parentheses and I have this One this equal greater than sign so it forms this nice looking arrow sort of and then 77 So that is my key and my value my key is one and the value it holds is 77 Yeah, I have a key of two and it's value 66 and a key of three and it holds a value one So that'd be a first example of a dictionary and if we run it we see there It's a dictionary and what it found was was these in 64 bit integer key and Value values if I can put it like that now We can use a different notation than this equal sign and Greater than sign by using the pair function So if I pair one and one hundred that means the one will be the key 100 will be the value Only because it is inside of a dict function So let's run that and we see we get exactly the same kind of notation on the output The key two has a value 200 the key three has a value 300 and the key one has a value 100 Now let's Do something else. Let's have a look at this I'm going to create this new dict And it is a dick, but I'm going to specify The data types the type that it can hold and it's any any so the key can be any and the value can be any So I have three keys here two three and one two and three and I have values 77 66 and then the string three And all of that is legal because I have specified the fact that both the key and the values can hold Any type or the type any? Let's look at dick three. I'm doing exactly the same thing, but look at this. I've got keys now Again, it's any any as I specified, but yeah, I've got a string as a key And here I've got a tuple actually as a key So all of this works. I'm going to have let's just run that and you'll see So I have a tuple as a key and a string as a value Yeah, I have a string or at least it is a string because it's in double quotation marks And not in single quotation marks, which make it a character So there's a string and it holds a value one, which is a 64. Well, at least an integer Now there are other ways to to specify what key values are and this is the symbol notation where I use colon and A word or a letter as we've done here And if I execute that code, we'll see this symbol notation that c the key c holds the value 309 b 305 a 300 and I can assess the the value In a dictionary by just referencing its key So the key is in the symbol notation for dict four, which would be colon c and again I get back the value 309 Now have a look at this using the get function for keys that do not exist So we have dict four remember that is so it only has the symbol values a b and c for the keys and the values 305 and 309 respectively But I'm going to use this get function takes the arguments dict four and then An h here as the key and I'm using symbol notation So it's colon h and just in case it doesn't exist because here we called c and c did exist But what if I called something and I'm not sure Whether that key actually exists I can build in this error code that says that key does not exist You could put anything in those quotation marks, of course So if I do call that it says that key does not exist so that I don't help my execution I don't get an error back. I've actually Allowed for this but for that you have to use the get function Now the b does exist So in this case Julia will ignore this this does not exist because the key b in symbol form does exist And it just gives me that value back So I can check if a key value pair exists and I use the in function So I've got to use this whole key and value here inside of parentheses. So in symbol form the key a is 300 And I'm checking whether that does exist. So the whole key value pair that exists and indeed it is true I can check to see if a value If a value actually has a key Or at least the other way around To check if a key exists So I can just the has key for And the d there that's going to return a false for me And I can add a key and a value to the dictionary very easily I'll just reference the the dictionary there dick for Give it give a new key value a key name I should say and add a value as simple as that I can just add it like that And if I have tick for now, we see that the d does exist now dictionaries are mutable i.e. The the values can be changed So remember we had c before but now I'm going to set the value to a thousand And if I do that and just check we'll see that c indeed now holds a value of a thousand I can also use the delete function So I can say delete and that's a bang so that makes it Permanent so dick for please remove key With a value a and if I do run it now that key and its value is gone It is gone So I can find out a little bit more information about a dictionary So I can ask for the keys that exist in dict for and there we go We see it's c b and d that are left after my bang deletion there And the values in dict for It also has a length just as tuples do and just as arrays do a length of tick And at the moment that is three But it has no size and it has no dimensions. So we can't do that So what we can do is say for k and v in dict for because remember there are two values there There's a key and a value value. So just using k and v in dict for print line The key has a value and I'm using the dollar notation there just to reference those values So the so that just tells you every time we run through all the elements in dict for if I can call them that There will be two values a key value and a value value And we can run through that simple for loop to get that now a for loop that can also be used to populate a dictionary. So here I have an array of names June Mary and John The string values Square brackets this makes it an array. I've given it the computer variable name called name underscore vowels and I'm creating this dict And it is a dict by using this dict function I'm giving it values so the the key can be of abstract string type And the values can also be accept abstract string and then actually empty parentheses. So it's an empty dictionary as it stands Then we're going to run through this for loop Is an in in enumerate name vowels now? What's happening here name vowels is a dictionary so we know it can take two values Hence I can run a for loop with two values and in enumerate enumerate just means run through this First the first element the second element and the third element. We've seen numeric enumerate before then name dict Then pass its index as the name x underscore dollar s So that's going to hold the value for the key. So it's going to be x underscore the key value and that equals in and in is the value That is past there Let's have a look at what this code does for us We've run it. Let's call name dict and look at look at that So we have x underscore and dollar s now s is just It runs through so don't get confused. I think I almost got I did quite get confused here. So it's the s It's just running through the indexes of this one two and three The n is going to actually hold those values So the name dict i'm going to have keys now named x underscore one two and three And they are going to hold the values That are listed here june mary and john So look clearly how this is structured and which way around the keys and values are used in this instance I can also iterate through a dictionary by key by the key value tuple So I say for k in keys Name dict So only the keys in name dict I'm going to just iterate through them and I can go into print k is And then name dict and use its key there so that I can get the value back So x one is june except two is mary except three is john Quite easy. I can also iterate through a dictionary by key and value separately We've done this before k and v separately Run through that and print that to the screen. So really nothing difficult there I can also sort a dictionary. Let's do that. So I've got dict five year. That's a dictionary It holds key values that are all abstract strings and Integers 16 bit integer values for the values So if a being one b being two c three, you see that etc Let's run that and we have dict five there But you see the entries are all jumbled up. They're certainly not in this order that I've done them And but I can use the sort Functions, I'm going to loop for k in the sort of the collection of the keys of dict five So this is almost Almost functional coding that I'm using. I'm using a bunch of functions to extract this A list of values through which I can iterate my for loop Let's execute that and see And that gives us an error at the moment It says that this value for dict or while loading in in this line the expression starting on line two It says undefined error dict is not defined Okay, so dict is there, but it should be dict number five. There's our error there Let's just execute that again. And now we see a is one b is two c is three d is four e is five f is six So I had to sort All of those values Now let's dissect this for loop a little bit Because it is as I said almost functional notation so Keys and dict five those are just going to be the keys a b c d a b c and def But I want to collect those so So We run through the values Each of them, but I want them sorted and because these are a b c d e f that is how it's going to be sorted So the keys are going to give us back the f c e b a d, but if I now collect those It's goes from look at what happened if we look at the type. This is a base key iterator for a dict But if I use the collect function on that it becomes a six element array Which means I can now iterate through it with a for loop And if I sort it it's going to do an alphabetical sort for me So you see how I used function upon function upon function to create this array of a b c d e f In the last section we're going to bring everything together I'm just going to do a little bit of text mining Just to show you how we can use The concept of collections just to to do a bit of Text mining