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
employeeshould not have the key"salary" - Use
delkeyword to remove the key
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules