ALL 11 Dictionary Methods In Python EXPLAINED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to be learning about all of the 11 methods that you can use with dictionaries in Python starting with number one which is the values method so first of all I'm going to create a sample dictionary that holds some users and some user IDs Now using the values method we can grab each one of the values from this dictionary so if we print the users.values we're going to get back an iterable that contains these dictionary values which you can use in a for Loop or anywhere that you can use an iterable moving on to number two keys keys do the exact opposite of the values method which means that if we were to print the uses dot Keys we would get back all the keys in our dictionary and just like with the values it's going to be an iterable of its own type which is called dictionary Keys number three pop pop allows us to remove a specified key just by passing it into our dictionary so if we type in uses dot pop and we want to remove James which has the key value of 2. then we can just do that by inserting two to pop and now if we were to print users after that we would only have Mario and Luigi something else that's nice about pop is that you can assign it to a variable so now we have popped and if we print popped we would get James back as a return it Returns the value of what we popped and to be consistent I would change this to a string also before I forget if you try to pop something that doesn't exist you're going to get a key error method number four pop item pop item is a lot like pop except all it does is pop the last item from our dictionary which means if we were to use it here like this we would remove James once again and we can actually call it two times if we want to continuously pop what's at the end of our dictionary so that would leave us with Mario if we used it two times but if we even use it let's say four times it's going to raise another key error because there's nothing to pop since the dictionary will have become empty method number five copy and for this method I'm going to be using a slightly different dictionary that contains a list of strings for each one of the keys now if you want to create a copy of this dictionary you can type in my copy or whatever variable name you want which will be of type dictionary and that's going to equal a sample dictionary dot copy now we can print our sample dictionary and the copy and you'll see that we will have created a copy of the sample dictionary and we can even verify that we created different copies by using the ID method in python as you can see they both have their own memory address which means they are separate copies but if you are experienced with python you will also know that we've only created a shallow copy so if we were to refer to my copy and we go to the key value of 0 at the index of 0 and we change that to three exclamation marks and then we were to print the sample dictionary and my copy you'll see that both of them changed because while the dictionary is unique or the copy of the dictionary is unique the references inside are not unique they still point to the same memory address for each one of the lists which means if we change a value in any one of these lists it's going to update throughout all the dictionaries if you want to prevent that behavior you're going to have to look into deep copy but deep copy does take a few more resources and can slow down your code base depending on what operations you might be performing number six get and I want to bring back my users so for users if you ever want to get a value from the dictionary without raising an exception I recommend you use the get method so here we're going to print users dot gets and we're going to get whatever has the key value of one and if we return that or or we print it we're going to get Luigi back now let's duplicate that and this time I'm going to insert 999 something that obviously doesn't exist in our dictionary and by default if you try to grab something that doesn't exist it's going to return none to us but we also have the option to specify what we should return in case the key is missing so I'm going to duplicate that and as the second argument I'm going to pass in the string of missing but you can insert anything you want here any variable you want and this will be returned if it cannot find that key so instead of none we got missing back number seven set default and set default is quite similar to using the get method and what it does is try to grab a key value pair and if it doesn't exist it will create that key value pair so to demonstrate this first we're going to use users dot set default and here we want to grab 0 otherwise we're going to set the value of the three question marks because it doesn't exist and if we run that we will get Mario back obviously but if we were to try to grab the value of 999 that does not exist so if we run this we're going to get three question marks back because now we created that key value pair and trying to search for 999 will return to us this string here and it's maybe a better idea to demonstrate this by printing our users as you can see as soon as we try to grab the key value of 999 it created that and updated our dictionary with that key value pair moving on we have something called clear so in case you get tired of all the key value pairs you have in your dictionary and you just want to start afresh you can type in uses dot clear and the next time you look at your users you'll notice that all of them disappeared because we cleared the dictionary we removed everything from that dictionary next we have the from Keys method and to use it we need an iterable so in this example I have Mario Luigi and James which is a list of type string now the users are also going to be a dictionary but that's going to equal the dictionary and here we're going to use the from Keys method and inside here I'm going to pass in the people now if we were to print these users you might be wondering what's the value going to be if we passed in D people well the value by default is set to none so we simply created a dictionary with the placeholder of none for each one of the keys but in case you don't like that you can also specify a default value for each one of the keys so instead of having none we can also say unknown and if we run that each one of the keys is going to be associated with the value of unknown so you can pass in any default value you want number 10 items so going back to our users we're going to print our users dot items and what it's going to give us back is an iterable that contains tuples of the key and value pair for each one of the items inside our dictionary and as I mentioned before you can use it as a simple iterable and to demonstrate this I'm going to be using it in a for Loop that says four key and value in users.items print the key and the value and if we run this we're going to get the key and the value for each one of these items and finally we've made it to the last method so using updates allows us to further modify or to actually expand our dictionary with new key value pairs so here we can call users dot update and we can pass in a new dictionary so as Bob and James's sister and the reason I added Bob with the key of two is because I want to show you what happens when you have clashing keys because James has an ID of two and Bob has the ID of two so what's going to happen well by now you should know that dictionaries can't contain the same key because they will clash and it just doesn't make sense so if you were to insert clashing Keys the last key that you were to insert will override the previous so James will not exist anymore but Bob will and James's sister will also exist but we also have another option for updating our dictionary and this is using the union operator which if I remember correctly is a recent Edition but recently in Python you can type in something such as print uses pipeline which will be used as a union operator followed by the dictionary you want to use to expand your current dictionary and I'm going to paste in spam and eggs and just like that we were able to expand our dictionary using a pipeline and for those of you who are very hardcore you can even use the pipeline followed by an equals to do an In-Place operation so here if we were to paste in spam and eggs and we were to print the uses this would perform an In-Place assignment which we could then use later so update is just the older version of this new syntax anyway those were the 11 methods that you can use with dictionaries do let me know in the comment section down below if I missed anything or if you learned something new regarding dictionaries I would love to hear about that in the comment section down below but otherwise with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 53,138
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: u0yr9B3nH8c
Channel Id: undefined
Length: 9min 26sec (566 seconds)
Published: Mon Aug 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.