how to remove elements from dictionary in Python code example
Example 1: remove elements from dictionary python
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
print(squares.pop(4))
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}