Python | Remove item from dictionary when key is unknown
Dictionary is a collection which is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values. It is widely used in day to day programming, web development, and machine learning.
Let’s discuss the various ways to remove items from the dictionary when key is unknown.
Method #1 : Using naive + del
del
keyword can be used to inplace delete the key that is present in the dictionary. One drawback that can be thought of using this is that raises an exception if the key is not found and hence non-existence of key has to be handled.
# Python code to demonstrate how to remove # an item from the dictionary without knowing # a key using naive + del method # Initialising dictionary test1 = { "akshat" : 21 , "nikhil" : 22 , "akash" : 23 , "manjeet" : 27 } # Printing dictionary before removal print ( "Original Dictionary : " + str (test1)) # using naive + del method # remove key nikhil item_to_remove = 23 for key, item in test1.items(): if item is item_to_remove: del test1[key] break # Printing dictionary after removal print ( "Dictionary after remove is : " + str (test1)) |
Original Dictionary : {'akshat': 21, 'manjeet': 27, 'nikhil': 22, 'akash': 23} Dictionary after remove is : {'akshat': 21, 'manjeet': 27, 'nikhil': 22}
Method #2: Using dictionary comprehension.
# Python code to demonstrate how to remove # item from dictionary without knowing key # using dictionary comprehension # Initialising dictionary test1 = { "akshat" : 21 , "nikhil" : 22 , "akash" : 23 , "manjeet" : 27 } # Printing dictionary before removal print ( "Original Dictionary : " + str (test1)) # using dictionary comprehension method # remove key akash value_to_remove = 23 res = {key: value for key, value in test1.items() if value is not value_to_remove} # Printing dictionary after removal print ( "Dictionary after remove is : " + str (res)) |
Original Dictionary : {'nikhil': 22, 'akash': 23, 'akshat': 21, 'manjeet': 27} Dictionary after remove is : {'nikhil': 22, 'manjeet': 27, 'akshat': 21}
Method #3: Using naive + pop()
+ naive
Python language specified pop()
for almost all containers, be it list, set etc.
# Python code to demonstrate how to remove # item from dictionary without knowing key # using naive + pop() # Initialising dictionary test1 = { "akshat" : 21 , "nikhil" : 22 , "akash" : 23 , "manjeet" : 27 } # Printing dictionary before removal print ( "Original dictionary : " + str (test1)) # using naive + pop() # remove key akash value_to_remove = 23 for key in test1.keys(): if test1[key] = = value_to_remove: test1.pop(key) break # Printing dictionary after removal print ( "Dictionary after remove is : " + str (test1)) |
Original dictionary : {'manjeet': 27, 'nikhil': 22, 'akshat': 21, 'akash': 23} Dictionary after remove is : {'manjeet': 27, 'nikhil': 22, 'akshat': 21}
Recommended Posts:
- Python | Ways to remove a key from dictionary
- Python | Remove spaces from dictionary keys
- Python | Pandas Series.item()
- Python program to sort a list of tuples by second Item
- Python | Inserting item in sorted list maintaining order
- Python Dictionary
- Python | Get key from value in Dictionary
- Python | Get key with maximum value in Dictionary
- Second largest value in a Python Dictionary
- Python Dictionary | pop() method
- Iterate over a dictionary in Python
- Python | Accessing Key-value in Dictionary
- Python dictionary | values()
- Python | Add new keys to a dictionary
- Add a key:value pair to dictionary in Python
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.