Python | Remove Keys from dictionary starting with K

Sometimes, while working with dictionaries, we can have a particular use case in which we need to remove certain keys. There can be many criteria upon which we may need to perform this task. One such criteria can be removal on basis of starting substring. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using Naive method + startswith() + pop()
This particular task can be performed using combination of above functions, which is a brute force method to perform this task. The pop function is used to remove the key value pair and startswith provide the condition upon which this has to be performed.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove Keys from dictionary starting with K
# Using Naive Method + startswith() + pop()
  
# initializing dictionary
test_dict = {"Apple" : 1, "Star" : 2, "App" : 4, "Gfg" : 3}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing Substring 
K = "Ap"
  
# Using Naive Method + startswith() + pop()
# Remove Keys from dictionary starting with K
res = list(test_dict.keys())
for key in res:
  if key.startswith(K):
    test_dict.pop(key)
  
# printing result 
print("Dictionary after key removal : " + str(test_dict))

chevron_right


Output :

The original dictionary is : {'Apple': 1, 'Star': 2, 'App': 4, 'Gfg': 3}
Dictionary after key removal : {'Star': 2, 'Gfg': 3}

 

Method #2 : Using list comprehension + dict() + startswith() + items()
There is also 1 liner alternative to perform this particular task in which we get all the keys and values using items() and then reconstruct the dictionary with keys that do not start with K using startswith function. The dict function at the end performs the conversion from list to dictionary.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove Keys from dictionary starting with K
# Using list comprehension + dict() + startswith() + items()
  
# initializing dictionary
test_dict = {"Apple" : 1, "Star" : 2, "App" : 4, "Gfg" : 3}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing Substring 
K = "Ap"
  
# Using list comprehension + dict() + startswith() + items()
# Remove Keys from dictionary starting with K
res = dict( [(x, y) for x, y in test_dict.items() if not x.startswith(K)] )
  
# printing result 
print("Dictionary after key removal : " + str(res))

chevron_right


Output :

The original dictionary is : {'Apple': 1, 'Star': 2, 'App': 4, 'Gfg': 3}
Dictionary after key removal : {'Star': 2, 'Gfg': 3}


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.