In this tutorial we under to discuss about dictionary methods the dictionary clear method is used to
delete all of the items in the dictionary so it clears a dictionary next.
The get method the get method returns the value for a key if it exists in the dictionary so let's check
this in the Jupiter notebook.
We have discussed before that if we want to retrieve or access any value in a dictionary we can parse
the corresponding key in square brackets like this.
So if we pass the key a one it retrieves the corresponding value that has been mapped to a 1.
Apart from this we also had the get method using the get method we can access these values in the dictionary.
So in order to apply the get method on the dictionary dict to 1.
This is defined my dict one dot get and the argument is the key that you want to access in the dictionary.
So I want to access the value corresponding to the key a 2.
So let's execute this.
So this is the value that has been mapped to the key A2 frozen foods.
Say for example you pass a key that is not present.
That is not existing in the dictionary so letters was b to as the key and execute it the get method
does not find the key that has been passed as an argument in the dictionary dict 1.
It does not raise an edit but it returns None.
Dickey is not found we can pass an optional argument that is returned.
If the key is not found in the dictionary in the get method.
So let's see this dict one board get and let us specify the key is B to and the optional argument would
be C minus one.
If the value is not found they get method would at dawn this value.
The default value select execute this line of code.
So since Dicky B2 is not found do you get method returns the default value minus 1.
So in this case Python does not raise an exception but if you try to access any of the values using
the square brackets say for example let's pass the key B 2 which is not present in dict 1.
Here Python raises an exception which is the key error since B2 is not present in the dict 1 dictionary.
The next method is the items matter the items method returns a list of key value pairs in our dictionary.
So this is the format for the items method D door items.
So once you execute this then this is the output.
Here is a list of the items in the dictionary dict 1 and as you can see here the items method returns
a list of tuples containing the key value PEDs in the dictionary dict 1 and the first item in each tuple
is the key and the second item is the value and we have the method keys and use the keys method returns
a list of keys in a dictionary whereas the right use method returns a list of values in a dictionary.
This is how you apply keys method on the dictionary.
It has written a list of keys that are present in this dictionary.
Similarly we have the values method which returns a list of values that are present in the dictionary.
The one next is the pop method.
The part method removes a key from a dictionary.
If it is present and returns it value so I am now calling the pop method on the dictionary dict 1 and
in passing the key a 4 so pop method is going to remove this entry from the dictionary Dec. 1.
Let us now display the dictionary dict one as you can see a Ford has been removed from the dictionary
say for example we want to pop an item which is not present in the dictionary say very positive key
B to which does not exist in the dictionary dict 1.
So the Bob method returns the exception key error the can also pass a default value that would be returned
if the key is not existing in the dictionary similar to what we have been in the get method so dict
one DA Bob let pass be 2 which is not existing in the dictionary and let's see if it does not find this
value though output should be minus 1 so instead of raising an exception or throwing an error the pop
method returns the default value minus one that we have past as an argument.
Next is the pop item method it removes a key value pair from a dictionary.
So when you call depart by the method on a dictionary the method pop item removed as a random or arbitrary
key value pair from the dictionary so if 5 has been removed the key a 5 it now only has the dictionary
dict one only has three keys a one a two and a three next as the update method the update method is
used to march or dictionary but another dictionary or with an eye dribble of key while you bet.
Say for example I want to march the dictionary is dict 1 and 2 using the update method.
So in this case you need to parse the dick to dictionary as an argument to the update method let's execute
this line and then check if the dictionary dict 1 has been updated.
OK as you can see the key value pairs in the dictionary d 2 has been added to dictionary dict 1.
Say for example if a key is present in both the dictionary is dict 1 and Dec. 2.
In that case the corresponding value indicate one for that key is updated to the value from dict 2.
You can use the delete statement to delete any of your own key value pairs from a dictionary.
So let's say you want to delete the key rate from the dictionary dict 1 so you can use the delete.
Let's just play one so here you have the updated dictionary dict one bit with the key weight delete
it we can use the Lent function to know the land of the dictionary so let's parse dict 1 to the length
function.
So there are seven keys in this dictionary.
So the length function has returned seven and this brings us to the end of the topic.
Dictionary methods.