The Wayback Machine - https://web.archive.org/web/20230107174224/https://www.geeksforgeeks.org/python-remove-multiple-keys-from-dictionary/
Skip to content
Related Articles

Related Articles

Python | Remove multiple keys from dictionary

Improve Article
Save Article
  • Last Updated : 08 Sep, 2022
Improve Article
Save Article

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 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))
 
# Remove multiple keys from dictionary
for key in rem_list:
    del test_dict[key]
 
# printing result
print("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 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))
 
# Remove multiple keys from dictionary
test_dict = {key: test_dict[key]
             for key in test_dict if key not in rem_list}
 
# printing result
print("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 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))

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 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))

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
Related Articles

Start Your Coding Journey Now!