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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

Original dictionary : {'manjeet': 27, 'nikhil': 22, 'akshat': 21, 'akash': 23}
Dictionary after remove is : {'manjeet': 27, 'nikhil': 22, 'akshat': 21}


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.