We saw in the previous posts how to retrieve items from Dictionaries. Now, let’s learn other operations associated with Dictionaries in Python.
Length of Dictionaries
We have created a dictionary called as personDict. We can find the length of Dictionaries as shown below.
Example:

Output:
This will display the number of items in a dictionary. Hence, the output is 2.

Addition of Items in Dictionary
Let’s see how we can add items to the existing dictionary “personDict“
Example:

In the line, personDict[“age”] = 30, i am adding new key called age to the personDict with the value 30.
I am printing the dictionary if the new item is added. Let’s see the output.
Output:

In the output, we can see that the new key ‘age‘ with the value ‘30‘ is added to the dictionary.
Removing items from the Dictionary:
We saw how to add items to the dictionary. Now, let’s try to delete the items from the dictionary.
Let’s try to delete the key “age” added to the dictionary. We need to use “pop” keyword to perform deletion.

we need to pass the key to the pop function such that it deletes the key and the value associated with it.
Output:

We can see that the age key is removed by using pop function.
There is another function called as popitem() which would delete the last item in the dictionary.
Example:

Output:
