Python | Remove item from dictionary when key is unknown

28 Jan, 2019
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 dictionarytest1 = {"akshat" : 21, "nikhil" : 22, "akash" : 23, "manjeet" : 27}  # Printing dictionary before removal print ("Original Dictionary : " + str(test1))  # using naive + del method# remove key nikhilitem_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 dictionarytest1 = {"akshat" : 21, "nikhil" : 22, "akash" : 23, "manjeet" : 27}  # Printing dictionary before removal print ("Original Dictionary : " + str(test1))  # using dictionary comprehension method# remove key akashvalue_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 dictionarytest1 = {"akshat" : 21, "nikhil" : 22, "akash" : 23, "manjeet" : 27}  # Printing dictionary before removal print ("Original dictionary : " + str(test1))  # using naive + pop()# remove key akashvalue_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}

Python dictionary-programs
python-dict
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
Introduction To PYTHON
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list