del my_dict["key1"]
If you have a dictionary named my_dict and you want to remove the key "key1", you can use the del statement. This will remove the key-value pair from the dictionary, and the key will no longer be accessible. Keep in mind that the del statement will raise a KeyError if the key does not exist in the dictionary. You can avoid this error by using the dict.pop() method, which will remove the key-value pair from the dictionary and return the value associated with the key, or a default value if the key does not exist. Here's an example of using dict.pop() to remove a key from a dictionary:
my_value = my_dict.pop("key1", None)
my_key, my_value = my_dict.popitem()
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.