my_dict = {'a': 1, 'b': 2, 'c': 3} del my_dict['b'] print(my_dict)
{'a': 1, 'c': 3}
You can delete an element from a dictionary in Python by using the del statement and specifying the key of the element you want to remove.
The above code will remove the key-value pair where the key is 'b'.
Alternatively, you can also use the pop() method with the key of the element as an argument. For example:
my_dict = {'a': 1, 'b': 2, 'c': 3} my_dict.pop('b')
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.