Python | Remove multiple keys from dictionary
While working with Python dictionaries, we can have a utility in which we require to remove more than one key at once. This type of problem can occur while working in the Web Development domain with NoSQL Databases. Let’s discuss certain ways in which this task can be performed.
Remove multiple keys from a dictionary using del
Here, we are using a Python loop to iterate rem_list, while iterating if a key matches with the test_dict we delete it using the del keyword.
Python3
# initializing dictionarytest_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}# initializing Remove keysrem_list = ['is', 'for', 'CS']# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Remove multiple keys from dictionaryfor key in rem_list: del test_dict[key]# printing resultprint("Dictionary after removal of keys : " + str(test_dict)) |
Output:
The original dictionary is : {‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Gfg’: 1, ‘CS’: 5}
Dictionary after removal of keys : {‘best’: 3, ‘Gfg’: 1}
Remove multiple keys from a dictionary using not in + dict comprehension
Here, we are using dict comprehension and if condition to filter out the keys that have to remove from test_dict.
Python3
# initializing dictionarytest_dict = {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}# initializing Remove keysrem_list = ['is', 'for', 'CS']# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Remove multiple keys from dictionarytest_dict = {key: test_dict[key] for key in test_dict if key not in rem_list}# printing resultprint("Dictionary after removal of keys : " + str(test_dict)) |
Output:
The original dictionary is : {‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Gfg’: 1, ‘CS’: 5}
Dictionary after removal of keys : {‘best’: 3, ‘Gfg’: 1}
Remove multiple keys from a dictionary using pop()
In this method, we just use the Python pop() function which is used to remove a single key along with the list comprehension which iterates for the entire list to perform the remove operation.
Python3
# initializing dictionarytest_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}# initializing Remove keysrem_list = ['is', 'for', 'CS']# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Using pop() + list comprehension# Remove multiple keys from dictionary[test_dict.pop(key) for key in rem_list]# printing resultprint("Dictionary after removal of keys : " + str(test_dict)) |
Output:
The original dictionary is : {‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Gfg’: 1, ‘CS’: 5}
Dictionary after removal of keys : {‘best’: 3, ‘Gfg’: 1}
Remove multiple keys from a dictionary using items()
In this method, rather than the removal of keys, we reconstruct the dictionary using the dict function, by extracting key and value pairs using items() and iterating over them using list comprehension.
Python3
# initializing dictionarytest_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}# initializing Remove keysrem_list = ['is', 'for', 'CS']# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# Using items() + list comprehension + dict()# Remove multiple keys from dictionaryres = dict([(key, val) for key, val in test_dict.items() if key not in rem_list])# printing resultprint("Dictionary after removal of keys : " + str(res)) |
Output :
The original dictionary is : {‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Gfg’: 1, ‘CS’: 5}
Dictionary after removal of keys : {‘best’: 3, ‘Gfg’: 1}






Please Login to comment...