How to remove a specific element from a dictionary in Python?
You can use the `del` keyword or the `pop()` method to remove a specific element from a dictionary.
Delete specific elements using the del keyword.
my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['b']
print(my_dict) # 输出 {'a': 1, 'c': 3}
Remove a specified element using the pop() method.
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.pop('b')
print(my_dict) # 输出 {'a': 1, 'c': 3}
The pop() method can also be used to simultaneously retrieve the value of the deleted element.
my_dict = {'a': 1, 'b': 2, 'c': 3}
deleted_value = my_dict.pop('b')
print(deleted_value) # 输出 2
print(my_dict) # 输出 {'a': 1, 'c': 3}