Transcript for:
Exploring Python Dictionaries Basics

hey everybody in today's video I'm going to explain dictionaries a dictionary is one of the four basic collection types for beginners a dictionary consists of key value pairs they are ordered and changeable no duplicates allowed a few examples of key value pairs could be an ID and a name an item and a price but in today's example we'll create a dictionary of countries and capitals let's name our dictionary capitals capitals equals in close your dictionary with the set of curly braces much like what you do with sets the first country will be the USA to add a value to this key type colon then some other value the capital of the USA will be Washington DC separate each key value pair with a comma then we can add another key value pair so the capital of India that will be New Delhi we'll add two more China the capital is Beijing Russia the capital is Moscow I think that's good enough just as a reminder if you would like to see all of the different attributes and methods of a dictionary you can use the Der function pass in your dictionary capitals then we'll need to print this here's all the different attributes and methods of a dictionary if you would like an in-depth description of all these attributes and methods you can use the help function uh that's herp help there we go so yeah that's just a reminder all right let's go over a few of the methods two get one of the values from a dictionary you would get the key type the name of the dictionary capitals dot get let's get the capital of the USA then we'll print it the value associated with this key the USA is Washington DC if I picked a different country like India well then we would get that Associated value which is New Delhi another thing if python doesn't find a key this is what will be returned let's get Japan which is not in our dictionary this method would return none we can use this within an if statement if capitals dot get Japan if a value is returned then we will print that Capital exists else we will print that capital doesn't exist so Japan is not in our dictionary that Capital doesn't exist but Russia is that Capital does exist that's how to check to see if a key is within our dictionary you can use the get method All Right Moving On Let's update our dictionary capitals dot update So within a set of curly braces I will add a key then a value Germany followed by Berlin then let's print our dictionary I'll use a print statement print capitals yeah and there's Germany right there using the update method we can insert a new key value pair or update an existing key value pair let's also change one of the existing values with our key USA let's update the capital to be Detroit yeah see the value has been updated the capital of the USA is now Detroit Michigan to remove a key value pair you can use the pop method then pass in a key let's remove China no longer exists within our dictionary it's gone you can remove the latest key value pair within a dictionary by using the pop item method capitals dot pop item with pop item you don't need to pass in a key pop item will remove the latest key value pair that was inserted then we have clear capitals dot clear that will clear the dictionary it's pretty self-explanatory the next few methods are a little tricky to explain to get all of the keys within the dictionary but not the values there is a keys method capitals dot keys I think I'm going to insert this within a variable Keys equals capitals dot Keys let's see what happens when we print this the keys method will return all of the keys within our dictionary technically Keys is an object which resembles a list I haven't discussed object oriented programming yet this is a little bit above our level if you ever need the keys in a dictionary you can use the keys method one use is that we can use that within a for Loop they're iterable for every key in capitals dot Keys method let's print every key if at any time you need to iterate over all the keys you can use a for Loop to iterate over every key that is returned from the keys method of your dictionary there's also the values method to get all of the values within your dictionary there is a values method values equals capitals dot values method then let's print our values like before with the keys method the values method will return an object which resembles a list let's iterate and print over every value within our dictionary for every value in capitals dot values print every value here are all the values within our dictionary this next one is probably the most tricky it is the items method capitals dot items I will assign what is returned to a variable named items then we will print items items returns a dictionary object which resembles a 2d list of tuples it's really complicated how might this be useful this time we're going to use a for Loop to print every key comma value in capitals dot items method we have in essence two counters this time I will print using an fstring every key value pair I will print every key as well as every value in our print statement so there's our dictionary laid out we have iterated over every key value pair it's kind of an advanced topic but I thought I would at least bring it up now so yeah that's a dictionary everybody it's a collection of key value pairs they are ordered and changeable no duplicates allowed you have a bunch of different methods such as get update pop pop item clear then you can get the keys the values or both which is the items method we'll be using dictionaries in a few game programs we'll be making in the future and well yeah those are dictionaries in Python