How to remove multiple keys from a dictionary in Python

However, there are many pythonic ways to remove multiple keys from a dictionary in Python. Here, this page will demonstrate the approaches to how you’d remove multiple keys from a dictionary. There are two common methods i.e, del(), and pop(). In the dictionary, these two methods are used when you want to remove the multiple keys. However, dict comprehension approaches are also demonstrated with example code to filter out multiple keys from a dictionary, on this page. 

If you want to learn more about Python Programming, visit Python programming tutorials.

Using a del() method to remove keys from dict in Python

To remove multiple keys from a dictionary in Python, the del() function is the first and easiest approach to do. the del() function removes the multiple keys from a dictionary in the easiest pythonic way. It will return the keys from the dictionary with values other than the removed keys. 

The execution process executes in the following manners:

  • Going with the non-dict comprehension method, with It’s for…in and if…in structure. 
  • Can’t say it dict comprehension, because it’s no more a one-liner pythonic operation. 
  • Now creating a dictionary in Python has multiple keys.
  • Now for making it computationally easy, make a list of keys that we want to remove from the original dictionary, and kept it in an object named “removing_keys ”. 
  • Now in the program body, use for…in structure to iterate “removing_keys“. If the keys in the “removing_keys” match with the keys in the “original_dict”, then with the help of the del() function, the task to remove multiple keys from a dictionary is accomplished.  
original_dict={'key1': 'Entechin', 'key2': 2, 'key3': 3, 'key4': 'Python'}
removing_keys = ["key2", 'key3']
for key in removing_keys:
   if key in original_dict:
       del original_dict[key]
print("remove_dict_keys: \n", original_dict)
remove_dict_keys: 
 {'key1': 'Entechin', 'key4': 'Python'}

Using a pop() method to remove keys from dict in Python

The pop() function does the same task as the del() function do in the dictionary in Python. With pop(), you can remove items by specifying their key names in the dictionaries. 

Here is the execution process goes in the following example code to remove multiple keys from a dictionary in Python:

  • Creating a dictionary with multiple keys first.
  • Then kept the keys to be removed from the original dictionary in a list named “removing_keys ”.

Using for…in structure in aggregation with pop() function, a task to remove keys from the original dictionary is performed pythonically. The pop() function removes the keys that have been specified in the list. The “original_dict” iterate over the “removing_keys” and remove the specific keys with the pop() function.

original_dict={'key1': 'Entechin', 'key2': 2, 'key3': 3, 'key4': 'Python'}
removing_keys = ["key2", 'key3']
for key in keys:
   original_dict.pop(key)
print("remove_dict_keys: \n", original_dict)
remove_dict_keys: 
 {'key1': 'Entechin', 'key4': 'Python'}

Using Dict Comprehensions

Using dict comprehension approach, it’s possible to remove the keys-values from the dictionary in Python in a one-liner pythonic way. 

Here is the execution process that executes the removal of multiple dictionary operations:

  •  Creating a dictionary containing multiple keys.
  • Then keeping removing keys in a list.
  • After then using a one-liner dict comprehension approach, accomplishing the task of removing multiple keys from a dictionary. 
  • Using for…in iterable structure in aggregation with if…not in conditional structure to perform the entire operation in a one-liner command. 
  • The for loop iterates over an if…not in structure and removes the keys from the dictionary. 
original_dict={'key1': 'Entechin', 'key2': 2, 'key3': 3, 'key4': 'Python'}
removing_keys = ["key2", 'key3']
print("original_dictionary:\n \t",original_dict)

First Approach:

print("remove_dict_keys:\n", {key: original_dict[key] for key in original_dict if key not in removing_keys})
original_dictionary:
 	 {'key1': 'Entechin', 'key2': 2, 'key3': 3, 'key4': 'Python'}
The second one-liner approach is dict comprehension in aggregation with items() method. items() function will iterate over the dictionary to remove multiple keys from the dictionary. The structure will return the key-values pairs of the items that have been filtered out after performing the if condition. If the condition satisfies, it will return the remaining keys-value pairs at the output.  

Second Approach:

print("remove_dict_keys:\n", {key: original_dict for key, original_dict in original_dict.items() if key not in removing_keys})
remove_dict_keys:
 {'key1': 'Entechin', 'key4': 'Python'}

Conclusion

Deleting multiple keys from a dictionary is fast and easy using Python’s del() and pop() functions. Additionally, two one-liners dict comprehension approaches have demonstrated with example code to remove the multiples keys from the dictionary. Now that you have practised it, you will be able to remove multiple keys from a Python dictionary. 

Leave a Comment

Your email address will not be published. Required fields are marked *