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.
# 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)) |
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.
# 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)) |
The original dictionary is : {'Apple': 1, 'Star': 2, 'App': 4, 'Gfg': 3}
Dictionary after key removal : {'Star': 2, 'Gfg': 3}
Recommended Posts:
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python | Remove spaces from dictionary keys
- Python | Remove multiple keys from dictionary
- Python - Remove Disjoint Tuple keys from Dictionary
- Python - Flatten and remove keys from Dictionary
- Python - Extract selective keys' values Including Nested Keys
- Python - Element Frequency starting with K in dictionary value List
- How to remove lines starting with any prefix using Python?
- Python | Split dictionary keys and values into separate lists
- Python dictionary with keys having multiple inputs
- Python Dictionary | keys() method
- Python | Add new keys to a dictionary
- Python program to Swap Keys and Values in Dictionary
- Python | Check if given multiple keys exist in a dictionary
- Python | Get dictionary keys as a list
- Python | Initialize a dictionary with only keys from a list
- Python | Ways to change keys in dictionary
- Python | Find keys with duplicate values in dictionary
- Python | Combine two dictionary adding values for common keys
- Python | Test if dictionary contains unique keys and values
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.

