Python | Remove multiple keys from dictionary

While working with Python dictionaries, we can have utility in which we require to remove more than one keys at once. This type of problem can occur while working in Web Development domain with NoSQL Databases. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using pop() + list comprehension
In this method, we just use the 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove multiple keys from dictionary
# Using pop() + list comprehension
  
# initializing dictionary
test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
  
# initializing Remove keys
rem_list = ['is', 'for', 'CS']
  
# printing original dictionary
print("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 result 
print("Dictionary after removal of keys : " + str(test_dict))

chevron_right


Output :

The original dictionary is : {‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Gfg’: 1, ‘CS’: 5}
Dictionary after removal of keys : {‘best’: 3, ‘Gfg’: 1}

 

Method #2 : Using items() + list comprehension + dict()
In this method, rather than 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove multiple keys from dictionary
# Using items() + list comprehension + dict()
  
# initializing dictionary
test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
  
# initializing Remove keys
rem_list = ['is', 'for', 'CS']
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Using items() + list comprehension + dict()
# Remove multiple keys from dictionary
res = dict([(key, val) for key, val in 
           test_dict.items() if key not in rem_list])
  
# printing result 
print("Dictionary after removal of keys : " + str(res))

chevron_right


Output :

The original dictionary is : {‘is’: 2, ‘best’: 3, ‘for’: 4, ‘Gfg’: 1, ‘CS’: 5}
Dictionary after removal of keys : {‘best’: 3, ‘Gfg’: 1}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.