Skip to content
Related Articles

Related Articles

Python | Remove item from dictionary when key is unknown
  • Last Updated : 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 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))
Output:



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))
Output:
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))
Output:
Original dictionary : {'manjeet': 27, 'nikhil': 22, 'akshat': 21, 'akash': 23}
Dictionary after remove is : {'manjeet': 27, 'nikhil': 22, 'akshat': 21}

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :