Python Tutorial for Beginners 5: Dictionaries - Working with Key-Value Pairs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody in this video we'll be learning about dictionaries and how we can work with them and Python so dictionaries allow us to work with key value pairs and if you're coming from another programming language then you may have heard these called hash maps or associative arrays so when I say that we'll be working with key value pairs these are two linked values where the key is a unique identifier where we can find our data and the value is that data so we can actually think of that almost like a real physical dictionary where we look up for definitions so in that example each word that we look up would be the key and the definition of that word would be the value so let's go ahead and take a look at some examples so let's say that we wanted to represent a student using a dictionary so to do this we could just create a student variable and set this equal to these curly braces and that's how we start our dictionary now within our curly braces here we will first put in our key so let's say that we want a key of name now we're going to put in a colon to separate our key from our value and for the name here we'll just start off with John and now let's add some more keys and values and to separate these keys and values we're going to put in a comma so next let's put in a key of age and a value of 25 and lastly we'll put in a key of courses and for this value we'll pass in a list of courses here so we'll say math and Comp Sci so now let's print out our student and see how that looks so if we save that and run it and we can see that it prints out all of our keys and values now let's just get a value of one key so to do this we can add square brackets after our dictionary and then specify the key that we want to access so I can put in square brackets here and let's say that we want to access the name of that student dictionary so I'll just pass in the name key and run that and we can see that that gave us that value of that name key now if I wanted the courses instead then I could just pass in that courses for the key if I run that then you can see that we got this list of math and Comp sign so we can see that these values and our dictionary can be just about anything our name is a string our age is an integer and the courses are list now all of our keys are currently strings but they can actually be any immutable data type so usually these would there be strings or injures but there are a few more data types that they can be as well so for example instead of name as our key here if for some reason we wanted this to be an integer so I'll just pass in one so a 1 is a valid key and now if I access that key of 1 and run that and you can see that that gave us John but I'm gonna set that back to be a string for now and keep that as name so what happens if we try to access a key that doesn't exist so for example I'll try to access the key of phone for a phone number so if we run this then we can see that we get a key error because that phone key doesn't exist now throwing an error when a key doesn't exist might not always be what we want so sometimes we might just want to return none or a default value so to do this we can use the dictionaries get method so instead of accessing this key this way if we were instead to say not get and use the get method so let's just go ahead and get the name since we know that that key already exists so if I run that then we could see that that works just like before and gave us the value of John but if I try to access a key that doesn't exist so we'll try to access that phone key again if I save that and run it then by default this get method returns none instead of an error and we can also specify a default value for keys that don't exist so to do this we can just pass the default value that we want as a second argument to this get method so I'll just put in a comma here and we'll put in a string that just says not found so if I save that and run it now we can see that four keys that don't exist it returns not found okay so let's look at how we can add a new entry to our dictionary so let's say that we wanted to add that phone number to our student dictionary and we'll set this just above our print statement here so to do this it's just as easy as saying student and then we will set the key that we want to set and set this equal to and we'll just set this equal to a string of five five five five five five five if I save that and run it then we can see that it found that value of the phone key when we ran our print statement now if a key already exists if we set its value like this then it will update the value of that key so for example if right below this I was to say student and name is equal to and we'll just pass in Jane if I save that and then a print out I'll comment out that for now if I print out our entire student variable then we can see down here that the value for name was updated when we assigned it to Jane now we can also update values using the update method now this is especially useful when we want to update multiple values at a time so for example let's say that we wanted to add this phone number update this name and also update the age as well so to do this all in one shot we could say student not update and this takes in a dictionary as an argument and the dictionary is just everything that we either want to add or update so we can say that we want to update that name to Jane and we will update the age to let's say 26 and we also want to add this phone key and that phone key will just set as what we had before five five five five five five five now if I save that and run it and we can see that just by running this one statement we updated the name to Jane the age is now 26 and it has this new key of phone number okay so now let's say that we wanted to delete a specific key and its value now one way that we can do this is by using the del keyword which stands for delete so let me just remove these updates here and then we can say so let's say that we wanted to delete the students age so it's as easy as just saying it del student age and now if we run this then we can see that now the only keys that exist are named and courses so that age key was deleted now another way that we can remove a key and value is with the pop method so if we remember from our video on lists the pop method will remove but also return and that value so that allows us to grab the removed value with a variable so we could say age is equal to and do a student not pop and what we want to pop is that age so if I save that then we'll also print the age here below student if I run that and we can see that the age and value were removed from our dictionary but we also created that age variable that contained the value that we popped off so that popped off 25 okay now let's look at how we can loop through all the keys and values of our dictionary so first if we want to see how many keys we have in our dictionary that we can just print out its length with the Elian function so if I was to say print le n of student and run that then we can see that it returns 3 because we have 3 keys and our student dictionary now if we wanted to see all of these keys then we could just print out student dot keys if I run that then we can see that that gave us all of the keys of our dictionary if we wanted all of our values then we could print out student not values if I run that you can see that that gives us only two values now if we wanted to see the keys and values then we could use this items method if I run this then we can see that now we have these pairs of key and value pairs so we have name John age 25 courses with the list and we'll be coming back to these pairs in just one second so if we wanted to loop through all of the keys and values in our dictionary we might be tempted to loop through the same way we loop through our list but if we just loop through our list without using any method then it'll just loop through the keys so for example if you were to say four key in student and then print out that key if we run this then we can see that it just looped through and printed out all of those keys now in order to loop through the keys and values we'll need to use that items method that we just saw a second ago and so we'll just plug that in there we'll say student items and now these come and a pair of two values so instead of just key we're also going to have to access the value so we can say four key value and Stu not items and now we'll print out the key and that value so if I run that so we can see that each loop through this key variable was equal to each key and this value variable was equal to each value okay so I think that's going to do it for this video I hope that now everyone feels comfortable working with dictionaries and the functionality that's available to us and in the next video we'll be going over conditionals and how to write if-else and Elif statements we'll also be learning more about boolean and boolean operators now if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you
Info
Channel: Corey Schafer
Views: 798,030
Rating: 4.9745851 out of 5
Keywords: Python, Python Dictionary, Python dict, Hashmap, Associative Array, Key-Value Pairs, Python For Beginners, Absolute Beginners, Python for Absolute Beginners, Python Basics, Python Data Types, Getting Started with Python, Python 3.6, Python 36, Python 3, python dictionary tutorial, python, dictionaries in python, python dictionary, python tutorial, python 3
Id: daefaLgNkw0
Channel Id: undefined
Length: 9min 59sec (599 seconds)
Published: Wed May 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.