REMOVING ITEMS FROM PYTHON DICTIONARIES
INTRODUCTION
There are three methods available in Python for removing items from the dictionary.
- del command
- pop() method
- popitem() method
- clear() method
del command
del command is used to delete an existing key from the dictionary.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 1
dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
del dict_class_wise_count['C']
del dict_class_wise_count['E']
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'D': 400}
In the above example, we have deleted the keys C and D from the dictionary dict_class_wise_count by using del command.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 2
dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
del dict_class_wise_count['C']
del dict_class_wise_count['D']
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 2
dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
del dict_class_wise_prog['C']
del dict_class_wise_prog['D']
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'E': 'XML'}
pop() method
pop() method is used to remove a key from the dictionary specified in the parameter.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 3
dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
dict_class_wise_count.pop('C')
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'D': 400, 'E': 500}
In the above example, we have passed the key C as the parameter to pop() method. In the output the C key has been removed.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 4
dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
dict_class_wise_prog.pop('C')
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'D': 'PHP', 'E': 'XML'}
popitem() method
popitem() method is used to remove an item from the end of a dictioanry.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 5
dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
dict_class_wise_count.popitem()
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400}
In the above example, we have used the popitem() method which has removed the last item from the dictionary.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 6
dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
dict_class_wise_prog.popitem()
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP'}
clear() method
clear() method is used to remove the entire dictionary and clear the memory.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 7
dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
dict_class_wise_count.clear()
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}
List of key value pair of dict_class_wise_count -> {}
In the above example, we have used the clear() method against the dictionary dict_class_wise_count and it has removed all the items from the dictionary and empty dictionary has been returned.
# REMOVING ITEMS FROM PYTHON DICTIONARIES
# Example 8
dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
dict_class_wise_prog.clear()
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
REMOVING ITEMS FROM PYTHON DICTIONARIES : Output
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}
List of key value pair of dict_class_wise_prog -> {}

RELATED TOPICS:
- INTRODUCTION TO PYTHON DICTIONARIES
- PYTHON NUMBER KEY VALUE IN DICTIONARY
- PYTHON STRING KEY VALUE IN DICTIONARY
- PYTHON MIXED KEY VALUE IN DICTIONARY
- PYTHON NESTED DICTIONARIES
- ADDING NEW ITEMS IN PYTHON DICTIONARY
- UPDATING PYTHON DICTIONARIES
- PYTHON DICTIONARY SORTING OPERATIONS
- PYTHON DICTIONARY BUILT IN FUNCTIONS
- PYTHON DICTIONARY SHALLOW COPY OPERATION
- PYTHON DICTIONARY DEEP COPY OPERATION