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.
# 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 | Remove multiple keys from dictionary
- Python | Remove spaces from dictionary keys
- Python | Add new keys to a dictionary
- Python | Minimum value keys in Dictionary
- Python | Get all tuple keys from dictionary
- Python | Get the number of keys with given value N in dictionary
- Python | Get total keys in dictionary
- Python | Add keys to nested dictionary
- Python | Get dictionary keys as a list
- Python | Count keys with particular value in dictionary
- Python Dictionary | keys() method
- Python | Grouping dictionary keys by value
- Python | Extract specific keys from dictionary
- Python | Unpacking dictionary keys into tuple
- Python | Ways to change keys in dictionary
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.


