Dictionary is used in manifold practical applications such as day-day programming, web development, and AI/ML programming as well, making it a useful container overall. Hence, knowing shorthands for achieving different tasks related to dictionary usage always is a plus. This article deals with one such task of deleting/removing a dictionary key-value pair from a dictionary, we will discuss different methods to deal given task, and in Last we will see how can we delete all keys from Dictionary. Show
Example: Before remove key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21} Operation Perform: del test_dict['Mani'] After removing key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22} Method 1: Remove a Key from a Dictionary using the delThe del keyword can be used to in-place delete the key that is present in the dictionary in Python. One drawback that can be thought of using this is that it raises an exception if the key is not found and hence non-existence of the key has to be handled. Demonstrating key-value pair deletion using del. Python3
Output : The dictionary before performing remove is : {'Arushi': 22, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Haritha': 21} Exception : Traceback (most recent call last): File "/home/44db951e7011423359af4861d475458a.py", line 20, in del test_dict['Mani'] KeyError: 'Mani' Method 2: Remove a Key from a Dictionary using pop()The pop() can be used to delete a key and its value inplace. The advantage over using del is that it provides the mechanism to print desired value if tried to remove a non-existing dict. pair. Second, it also returns the value of the key that is being removed in addition to performing a simple delete operation. Demonstrating key-value pair deletion using pop() Python3
Output: The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found Method 3: Using items() + dict comprehension to Remove a Key from a Dictionaryitems() coupled with dict comprehension can also help us achieve the task of key-value pair deletion but, it has the drawback of not being an in-place dict. technique. Actually, a new dict is created except for the key we don’t wish to include. Demonstrating key-value pair deletion using items() + dict comprehension. Python3
Output: The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21} The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22} Method 4: Use a Python Dictionary Comprehension to Remove a Key from a DictionaryIn this example, we will use Dictionary Comprehension to remove a key from a dictionary. Python3
Output: The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} Method 5: Iterating and EliminatingIn this example, we will use a loop to remove a key from a dictionary. Python3
Output: {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} {'Anuradha': 21, 'Mani': 21, 'Haritha': 21} How to Delete all Keys from a Dictionary?Method 1: Delete all Keys from a Dictionary using the delThe del keyword can also be used to delete a list, slice a list, delete dictionaries, remove key-value pairs from a dictionary, delete variables, etc. Python3
Output: {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Deleted! Method 2: Delete all Keys from a Dictionary using dict.clear()The clear() method removes all items from the dictionary. The clear() method doesn’t return any value. Python3
Output: {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Length 0 {} What does Del Do in Python dictionary?The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.
How do I remove something from a dictionary Python?You can use the following methods to remove items from a dictionary in Python:. The del keyword.. The clear() method.. The pop() method.. The popitem() method.. How does Del operation work on dictionaries?In a dictionary, Python del can be used to delete a specific key-value pair. For deleting a specific data element in the dictionary, the del statement only accepts the key as an index value.
How do I delete and remove an element from a dictionary program?Method 1: Remove a Key from a Dictionary using the del.. Method 2: Remove a Key from a Dictionary using pop(). Method 3: Using items() + dict comprehension to Remove a Key from a Dictionary.. |