1Like python lists and sets, python dictionaries uses also different methods. In this lesson we will focus on these python dictionary methods. We will give different examples for each method and after this lesson, you will be ready to use dictionary methods.
So, which methods are used by dictionaries? These methods are given below:
Now, let’s focus these python dictionary methods and learn them one by one.
get() method is used to get the value of a given key. As you know there are key:value pair in python dictionaries. With this method, we give the key and receive its value.
The above key pair exist already. We can use this method to receivea new defined key value like below:
The output of this python code will be:
keys() method is used to receive the keys as output. AS you know dictionaries consist of key:value pairs. With keys() method, we receive the keys define in a dictionary.
We can also add a new item in a dictionary. And if we assign keys to a variable, and after we add a new item to the dictionary, then the variable will also change and includes this new item. BElow, there is an example for this usage.
Like keys, we can also receive values as output with the help of values() method.
Again if we add a new ite mor if we change the value of an item, and we assign the values to a variable with values() method, the variable will include new values.
items() method gives all the key:value pairs. In other words, we get all items with this method. Here, be careful about the round and square pharantesis.
pop() method is used to remove the specified item from the dictionary. If we assign this method to a variable, and if we give any key as parameter, it returns as the deleted value of this key. After this process, if we print the updated dictionary, we do not see this item in the python dicitonary.
The output will be :
popitem() method is used to remove the last item in the dictionary. This is a method like pop method used with list and tuples. It is not similar with the pop method used with dictionaries.
Again, if we asign this method to a variable, the deleted item is assigned to the variable.
As we mentined above, the return of popitem() method will ve the deleted item like below::
setdefault() method is used to return with the value of a key if it exist in the dictionary.
If there is no items like it, then it ass this item and again returns with the value of the specified key.
One of the other python dictionary methods is update method. With update() method, we can insert new items to the dictionary.
As in other data types that stores data collections, clear() method is used to delete all the items in the dictionary. In other words it empties the dictionary.
With copy() method, we copy the dictionary and we can assign it to any variable or we can use it in any other place.
One of the other python dictionary methods is from keys() method. fromkeys() method is used to receive, specified keys and values.
Below, we will use two parameters. The first parameter will give key and the second oen will give value. So, for eack key, we will have three values.
In this lesson, we have learned different python dictionary methods. You can do more practice on each of these dictionary methods. With more practice, you will be more ready to use these methods in your coding examples.