Open In App

Python | Remove multiple keys from dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

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 : {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
Dictionary after removal of keys : {'Gfg': 1, 'best': 3}

Time complexity: O(n), where n is the length of the rem_list .
Auxiliary space: O(m), where m is the number of keys to be removed.

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 : {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
Dictionary after removal of keys : {'Gfg': 1, 'best': 3}

Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(m), where m is the number of keys to be removed from the dictionary

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 : {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
Dictionary after removal of keys : {'Gfg': 1, 'best': 3}

Time complexity: O(n), where n is the number of keys in the dictionary,
Auxiliary space: O(1), as the program only uses a constant amount of extra space to store the rem_list and some temporary variables.

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 : {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
Dictionary after removal of keys : {'Gfg': 1, 'best': 3}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary. The loop used in the list comprehension runs n times, and each iteration takes constant time to check if the key is in the rem_list and access the value associated with the key.

Auxiliary Space: O(m), where m is the number of keys to be removed from the dictionary. The space required to store the rem_list is O(m), and the space required to store the resulting dictionary is also O(m) because at most m key-value pairs are added to it.

Method 5: Using a for loop and the dict.pop() method

In this method, we use a for loop to iterate over the keys we want to remove and call dict.pop() method with that key to remove it from the dictionary. We pass None as the second argument to pop() method to handle the case when a key is not present in the dictionary.

Python3




test_dict = {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
 
rem_list = ['is', 'for', 'CS']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# remove multiple keys from dictionary using a for loop and dict.pop()
for key in rem_list:
    test_dict.pop(key, None)
 
# printing result
print("Dictionary after removal of keys : " + str(test_dict))


Output

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

Time Complexity: O(k), where k is the number of keys we want to remove from the dictionary.

Space Complexity: O(1), as we do not use any additional data structure to store the intermediate results.

Method 6 : Using a dictionary comprehension. 

 step-by-step approach:

  1. Create a new dictionary comprehension that includes only the key-value pairs from the original dictionary that do not have keys in the rem_list. This can be done by iterating over the items() of the dictionary and checking if the key is not in the rem_list.
  2. Assign the new dictionary comprehension to the original dictionary variable to update it with the new key-value pairs.

Python3




test_dict = {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
rem_list = ['is', 'for', 'CS']
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# remove multiple keys from dictionary using dictionary comprehension
test_dict = {k: v for k, v in test_dict.items() if k not in rem_list}
 
# printing result
print("Dictionary after removal of keys : " + str(test_dict))


Output

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

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), because a new dictionary is created and stored in memory.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads