 Right, so hey guys and welcome back to another piping tutorial so in this video We're gonna be learning how to use dictionaries in Python So you guys might be wondering what dictionaries are so just like variables. They're a form of storage I'm a bit more similar to arrays or lists in Python, but they can store a lot more and they're a lot more useful and handy So without further ado, let's begin first off like always. I'm gonna go ahead and create a new file on my desktop and call it Dictionary.py. You can call it whatever you like I'm just gonna go through some of the common methods that you can use on your dictionary and some of the different things that you can do using a dictionary in Python So first off, what is a dictionary? As I said, it's a form of storage so you can store information to it or data to it and Yeah, but it's a lot more than just a variable because you can store multiple fields of info information in it So let's assume that I had a Car factory and I wanted to store details about a car So if I wanted to store details about a car, I would have to do a couple of variables So I would have something like car name equals Ford car model equals Mustang and You guys get the general idea car. I don't know fuel equals petrol Yeah, so I'd have to do a lot of these so what? and So what the dictionary allows us to do is avoid having to do all of that so when we want to save a lot of information about a single Subject sort of like object oriented. We just we can just save it into one little dictionary So what do I mean by that? So let's say we had a So the company for the car would have been Ford. So Ford equals and then we use our curly braces, which is where our Fields for the Ford dictionary are going to be saved. So in here, I'm going to type in brand Which is obviously Ford So we're kind of following a key value kind of Structure over here. So the first thing that you enter is the key Then you use a colon and the second thing that you enter is going to be the value of that key So I'm going to enter a few more keys in here and you need to separate each key and value with a comma So brand I'm going to go with model Hold on model is going to be Mustang, not the best at cars. So I might be wrong here, but oh well, it's just for the tutorial I'm going to type in fuel to colon and then Cool. So instead of having to type three different variables, we've now got this all of this information About a single car. Let's actually just rename this to Mustang, let's just rename that to Mustang. So let's so instead of having to say car name equals Mustang car brand equals Ford and car model equals Mustang again and fuel type equals petrol. We've done it all using one clean variable So now you might say How do we actually access this information? So this is the first bit that we've learned how to data, so key and value Pairs is the main key word to take away here because we need to save with a key and then give that key a value So now if you wanted to find out What this looks like we're going to quickly print and see what it looks like so print Mustang run this and as you see right here As I said before it's stored as a dictionary and then we have a key value pair So we have the key called brand and the value of that is Ford We have a key called model value of that is Mustang We have a key called fuel and the value of that is petrol. Cool So what if we just wanted to access the brand and none of this stuff pretty easy stuff. So There's a couple of ways of accessing information. There's actually two. So two methods of accessing Information the first one is by doing print Then you need to mention the name of your dictionary So where your dictionary save minus a into a variable called Mustang and then I'm gonna go in and use my curly brace This is almost how we refer to an array object, but then you need to type in in here the actual key name So my key name is brand. So I'm gonna go into my Mustang Dictionary right here and then I'm referring to the key called brand So when I do that, it should return the value of it, which is Ford So if I run this now, it should say Ford, which is my second line It's giving me the first line because I've printed out the previous one as well So it says Ford now similarly if I wanted to access the other keys in there I could just type in instead of brand I could type in model and I should be presented with Mustang And instead of model I could just type in fuel and I should be presented with patrol So at this stage it kind of is evident that it's helping you manage code a lot more better And then it looks a lot more better as well It's more manageable because everything that's related to this one Object sort of thing is just in here and then you can change it as you wish just in one spot So now the second way of accessing information would be by using a dot-get method So you could do print cars dot get if you prefer the just the normal brackets then this method is recommended for you and then when you do dot get you just mention the key that you want to access and This you don't need to use square brackets so if you prefer this kind of syntax where you don't have to mess with the brackets go for it and We should have okay cars is not defined obviously because it's Mustang I don't know why that came to my head Mustang and it says Ford because we've referred to the brand of the car right here, which is why it says Ford Cool. That's the two methods of accessing information now, I'm just going to make a comment here saying two methods of accessing info or let's just say values Using keys because that's what we're doing. We're using the keys which are fuel and brand to actually Access their values from this dictionary called Mustang Cool So we've got a pretty solid understanding of how to create a dictionary and then how to use two methods to actually access information from it Now let's move on and learn how to actually update these values if we need it to So let's say we had a function where we wanted to update things. I'm just going to make a comment saying updating values of a dictionary Cool, and then let's say I wanted to update the Fuel type. Let's say a new model comes out in the fuel type changes to diesel Now I don't need to go ahead and do this directly in here I can do it down here by using the following lines of code. So I could do something like Mustang so first of all you need to use the name of your dictionary and then use square brackets and In the square brackets you want to mention what you're going to be updating So I'm going to be updating my fuel type and Then you use an equals to sign to actually reassign what the fuel to whatever value you want it to be So I want mine to be diesel at this point because I'm let's say it's just changed to diesel recently And then I'm going to go ahead and print the whole dictionary again. So Must act let's run this now and then it says two methods of accessing key that Values using keys, which is the previous one and then it's done what we've done next as well Which is changing and updating the fuel type to diesel So if you look right here brand is still the same model is still the same But the fuel has been updated to diesel due to this line right here. So I'm going to make a print line right here that's going to say Updating values of a dictionary Cool now, that's that let's move on to the next bit Also, if you want it to if you don't like the square brackets once again, you can use the Normal brackets approach so you can do something like If you do something like cars Get Fuel actually let's change brand this time brand Wise why did I type in cars? I keep typing that brand equals I Don't know has been changed And then we'll print it off Okay, so apparently we can't use that. So ignore what I just said. Sorry. That was my bad So it's apparently just one way of actually updating variables, which is the square bracket way that I just explained before So just ignore what I just said. Sorry about that Now we're going to move on to actually learning how to add additional key values on to the dictionary without actually having to manually do that so Dict or dictionary now if I wanted to add a new item All I do is I'm going to do something like When type in the name of my dictionary, which is Mustang and then the name of my new key Which is going to be let's say I'm going to store how many doors there are so doors and then I'm going to do equals Let's just say it has two doors and when I print my Mustang Dictionary again, it should have a new key value where it has a key of doors and the value is two print this quickly saying So that we actually know what part belongs where So this is the part we're looking for adding an item to the dictionary So we have brand forward model Mustang fuel diesel and then we have also appended a new key value onto it Which is doors to call pretty cool stuff. So we've been able to add an item to the dictionary So you guys might already realize that these operations or methods are very similar to an array type Because they are quite similar as well But you have a lot more advantage of using a dictionary, which I'm going to show you in a bit So let's say for example, you wanted to make a copy of the dictionary that you already have and save it into a new variable It's pretty simple. All you need to do is let's just say And then all you need to do is create a new variable, so I'm going to say Mustang Duplicate equals Mustang Mustang and then you use the dot copy method on it and then what it's going to do is it's going to take the current or most updated Mustang Dictionary and then assign that to your Mustang duplicate. I can prove that by printing the Mustang Duplicate dictionary out and then it should be the exact same of the Mustang copy So as you see right here making a copy of the dict It's literally the same as this one because they have copied each It has copied the Mustang and dictionary into the Mustang duplicate dictionary Oh, so that's how you make a copy. Now, let's learn about how to actually remove a key value if you need to And you are going to remove the key value by providing the name of the key So you need to first of all like always type in the name of the dictionary that you want to remove it from and then use the Dot-pop method so dot-pop and then I'm going to take off Doors because I don't want them anymore. I guess. Well, actually, let's just take off Take a few So when I take a few and I print Mustang again now just before doing that. I'm going to make another print statement. So I know where to look for the outcome of this Key value. Okay Let's run this and if you notice right here Removing a key value and the item or the key that we provided was few So anything with a key feel has been deleted from this dictionary and this is the most updated one So that's how you remove a key value from a dictionary using the key name now, let's move forward and Learn how to actually clear an entire dictionary. So make it empty just like it would be in the start. So Clearing a dictionary now when we clear a dictionary what we need to do is To do to do it's pretty simple type in the name of a dictionary and then dot-clear and now when I print Mustang it should just be an empty dictionary, but first off I'm going to print Clearing a dictionary cool, and as you see right here clearing a dictionary and we only have Curly brackets, which means the dictionary is empty. So that's how you clear a dictionary Now I'm going to censor this line right here I'm going to comment it out because I want the dictionary to actually remain populated for the next things that I'm about to do So what I'm going to do next is show you guys how to loop through the different keys That are in a dictionary so that you can use them for whatever you like So to loop through we're going to be using a for each loop so for X in Mustang Let's do something like print X Now some of you may think or get the idea that this is going to print out the values, but it's not it's going to print out the keys And if you see right here as I said before It says looping through the keys and then it only shows me the keys Brian model and doors and remember We took off the field. That's why it's not showing up now if you want to Actually get the values out of this we have another method to do so before that. There's also another method we can use to actually Actually never mind. Let's just skip to the for each For now on how to loop through the keys now. I'll show you how to loop through the values as well Now to loop through the values you have two methods Let's start with the first one. I can type looking through values of a dictionary And then I'm going to do for each again a for each loop for X in Mustang so for each key in the Mustang dictionary what we want to do is We want to print and Then we're going to print Mustang because that's the name of our dictionary We need the square brackets X So we're actually referring to the value and then this should print out the values for us And as you see right here where it says looping through values of a dictionary It's giving us the value so Ryan Ford model Mustang doors to perfect And there's also another way you can do this just in case you hate the square brackets approach You can do for X in Mustang Dot values. So this way you are actually capturing the value straight away instead of using an X later on So for X in Mustang dot values print X This way you're actually gonna object is not Object is not iteratable Why is that hmm so for X in Okay, so I forgot the brackets on the values Because it's a method and now here we are as you see right here We have four Mustang to using the first square bracket method And we also have four Mustang to same outcome using the dot values method if you hate using square brackets Or if your program just happens to need it for your logic to work Cool, I'm gonna create a new line and now in here. We're gonna do Looping through both Keys and values because sometimes you may need to loop through both the key as well as the value So to do so what we need to do is again once again, we're gonna be using a for loop so for X Come up why because we're we need two variables this time so X is going to be the key and Y is going to be the value in Mustang The items So for each item We need to print X comma Y so that should print out the key and the value So if I can maximize the screen it says brand forward model Mustang and doors to so it's printing the key And the value while iterating through each item in that dictionary. Oh, so that's how you do Accessing key and values by looping through them Now let's move on and learn how to actually check what the length of a dictionary is So to check the length or how many items are in a dictionary Dict we're gonna print A Dict and Then we're gonna do a simple function. So Mustang dot actually, we're just gonna do print Len and Then we need to do the name of our dictionary, which is Mustang Len just stands for Len Now when I print this out We have length of dict 3 Which is correct based on the last or most updated version of the dictionary. So brand model Brand model and I think it was doors. Yeah, so we have actually three key value pairs in there Which is why it's saying the length is three. Perfect. That's that now. We're gonna move on to learning how to nest dictionaries In my opinion, this is the most useful part of the tutorial Because this is what I use quite often now, you guys might say why are we learning this all of a sudden? I'm gonna say because it's very useful. It's gonna help you keep your code concise and also give you Hustle free variable management in this case because you won't have a lot of variable names instead You can just refer to the key value terms and just use a dictionary Nesting is going to be the most important part of this video. So I hope you're listening. So next thing dictionaries Oh, so to nest a dictionary, let's assume we had a factory like I said before but this time we have different manufacturers for the cars. So let's do all cars Because last time we only had one car, which was Mustang. Let's do all cars equals a Object. Oh wait, I keep calling it object because I'm used to JavaScript. So Sorry about that. It's actually a dictionary in Python. That's how they call it now all cars Equal to an empty dictionary for now. Now in here, I'm gonna mention my first car company, which is going to be Ford Now when I mention the value for Ford, I'm going to open up another dictionary in there Which means I can actually concatenate or just add more information in here. So I'm going to Later on populate that so I'm going to go comma and then I'm going to put my next brand Which is probably going to be Merck or Mercedes, but I just can't be asked to type it out Then that's going to be an empty dictionary, too If you see right here when nesting all of these dictionaries inside of our main dictionary called all cars Which is going to be really easy to manage later on once I've finished with this and then Volkswagen. Let's do as well That's about it And cool. So what we need to do now is actually go ahead and populate these Companies or car makes so in each of these Companies, we're going to be storing a key value pair of the year in which they were built Let's say the most recent of the Ford was 2010 Let's go down to Merck inside the Merck dictionary. Let's say the most recent year for Let's go down to Wolf's wagon Obviously none of this information is accurate. It's just for the sake of this tutorial Let's say the year for this was 2000. Cool Might look a bit confusing to the eye, but it's a lot more easier to manage So now I've got a nice little dictionary that's nested with other dictionaries So all cars pretty much includes information about Cars from all companies. So cars from Ford cars from Merck and cars from Wolf's wagon You can conquer. I mean, you can nest as many dictionaries inside the dictionary as you like as far as it doesn't get too confusing But I would recommend sticking to probably two or three. So now if I wanted to I can even add I don't know make and then whatever I could keep adding So to show you what it looks like, I'm going to go ahead and print the old cars dictionary so print all cars and As we expect it's going to show us all the information So I'm going to maximize the window and now over here as you see we have Ford which was the first key value pair Where we have and another dictionary that's assigned to our Ford Which is the year date in which the most recent car most recent car was released So we have year 10. Then we have information about Merck Where we have the year 2020 and then we have information about Wolf's wagon, which has the year 2000 So if you wanted to access this information separately, you can do it really easily So let's do let's say I wanted to access information about Ford. I could do print All cars and then I'm going to type in my Key name in here, which is Ford because I want to get information about Ford and Now if I run this up as you see right here I have the year 2010 because I am ringing up the information from my all cars Ford now Let's try something else. It might not work or it may work. Let's give it a shot anyway all cars and if I do Ford And if I do two more brackets to try and grab the year as well by itself Not 100% sure if it will work. Oh, and it does work. So it does work as expected So if you wanted to Find the year individually instead of having the key value show up you can look for Ford first Then when it's found Ford it's pretty much in this dictionary And then you want you can look for pretty much any key that you have in there So for example, if I had another one in here that said color Let's just make it black So If you wanted to access the color all you have to do For the Ford color is type in color in here So this will return the color of the four Ford model that you have stored in your dictionary to me on this and it should say black Perfect. So that's about it for today's tutorial guys Hope you guys have been able to learn quite a bit about dictionaries and how to use the different methods in it We're going to be using this information to actually create a rock paper scissors game very soon, which I'm going to release On the next tutorial, we're going to create a command line version for rock paper scissors first and then we're possibly going to move on to Creating a GUI if possible. I'm going to check if it's actually doable and if it is I'm going to post that tutorial next And that's about it. Thank you guys for all the support that you guys have been showing me so far I really appreciate it. If you guys could keep sharing my videos, that would be awesome if you guys would somehow like to Contribute and donate to the videos Straightaway you guys can do so by purchasing a super chat emoji or highlighted message While the video is premiering Also guys consider joining the discord chat, which is going to be linked in the description It's really fun in there and chill you can meet new people and also discuss programming ideas And if you like to you can also follow my socials and I will see you beautiful faces in the next tutorial. Peace