python remove from dictionary by value code example
Example 1: delete a key value pair from a dictionary in python
mydict = {'score1': 41, 'score2': 23, 'score3': 45}
del mydict['score2']
print(mydict)
{'score1': 41, 'score3': 45}
Example 2: how to remove dictionary entry in python
del dic[key]
Example 3: python delete value from dictionary
dict = {'an':30, 'example':18}
del dict['an']
dict.pop('example')
dict.pop('test', 'Key not found')
Example 4: python delete value from dictionary
dict.pop('key')
dict.pop('key', 'key not found')
Example 5: python dictionary delete by value
myDict = {key:val for key, val in myDict.items() if val != deletevalue}
Example 6: 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}