Editor settings

General settings

Save settings

Learn to program with Python

Deleting Dictionary Values #

In Python, you can delete a value from a dictionary using the del keyword followed by the key of the value you want to delete. Here's an example:

my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['b']
print(my_dict) # Output: {'a': 1, 'c': 3}

In the above example, we first define a dictionary my_dict with three key-value pairs. We then use the del keyword to delete the value associated with the key b. When we print the resulting dictionary, we could see that it contains only the key-value pairs for a and c.


Exercise #

Delete the key "salary" from the dictionary employee.

Tests #

  • The dictionary employee should not have the key "salary"
  • Use del keyword to remove the key
Output will be displayed here