remove entry dict python code example
Example 1: python remove a key from a dictionary
del dictionary['key']
dictionary = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
del dictionary['c']
dictionary
--> {'a': 3, 'b': 2, 'd': 4, 'e': 5}
Example 2: how to remove an element from dictionary using his value python
a_dictionary = {"one": 1, "two" : 2, "three": 3}
desired_value = 2
for key, value in a_dictionary.items():
if value == desired_value:
del a_dictionary[key]
break
print(a_dictionary)
--------------------------------------------------------------------------------
OUTPUT
{'one': 1, 'three': 3}