Dictionary Basics and Operations
We can delete key-value pairs using our old friend - the del statement. We simply specify the dictionary and the indexing operator for the key to be removed and pass it to the del statement. There is no need to know the value corresponding to the key for this operation.
A Targeted Removal
Suppose a dictionary contains several key-value pairs, but one pair is no longer needed. The relevant operation is not a search for the value. Instead, you identify the dictionary and specify the key that identifies the pair to remove. Python's del statement then deletes that key-value pair.
What do you think happens?
A dictionary contains the key name with the value Ada. To delete that pair, do you need to specify Ada as well as name?
Reveal answer
Answer: No, the key is enough.
The deletion operation specifies the dictionary and the key inside the indexing operator. It does not require the value corresponding to that key.
Before and After Deletion
Consider the generated example dictionary {"name": "Ada", "role": "designer"}. The key name identifies the value Ada, and the key role identifies the value designer. If the name pair is deleted, the resulting dictionary keeps the role pair and no longer contains the name pair.
The important change is the disappearance of the selected key-value pair. The other pair remains because the deletion targets the specified key rather than the whole dictionary.
The Dictionary-Key Path
The deletion form has three parts: the del statement, the dictionary, and the indexing operator containing the key to remove. In conceptual form, it is del dictionary[key]. The dictionary identifies the collection being changed. The key inside the indexing operator identifies the exact key-value pair targeted for removal.
A lookup is a dictionary operation that takes a key and finds the corresponding value. Deletion uses the same key-centered way of identifying the pair, but the purpose is to remove the pair rather than simply find its value.
A Complete Trace
Removing the status Pair
A generated dictionary is {"status": "active", "owner": "Mina"}. Identify the pair removed by del records["status"].
Identify the dictionary: records is the dictionary that will be changed.
Read the key: The key inside the indexing operator is status.
Locate the pair: The key status corresponds to the value active, so the targeted pair is status with active.
Apply del: The del statement removes that key-value pair. The value active does not need to be supplied separately.
Inspect what remains: The owner pair remains in the dictionary.
The status-active pair is removed, leaving the owner-Mina pair.
| Deletion target | What identifies it | Is the associated value needed? |
|---|---|---|
| One key-value pair | The dictionary and its key | No |
| The corresponding value | Not the deletion form described here | The operation is key-based |
Mistakes to Avoid
Trying to delete by writing the value instead of the key.
The deletion form identifies the pair through the key inside the indexing operator.
Fix:
Use the dictionary and the key that identifies the pair.Assuming the entire dictionary is removed.
The operation targets one specified key-value pair.
Fix:
Expect the other key-value pairs to remain.Believing that the corresponding value must be known first.
The source description explicitly states that the value corresponding to the key is not needed for this operation.
Fix:
Identify the key and use the dictionary-key form with del.
When tracing a deletion, say the operation aloud in this order: dictionary, key, targeted pair, remaining pairs. This keeps the key-based nature of the operation visible and prevents the value from being mistaken for the deletion selector.
Practice Check
A generated dictionary contains {"language": "Python", "level": "beginner", "format": "course"}. You want to remove the language key-value pair. State which key identifies the pair, whether you need to know the value, and which two pairs remain afterward.
Hints
- The deletion target is identified by the key inside the indexing operator.
- The value is not required for this operation.
- Only the specified key-value pair is removed.
Expected reasoning: language identifies the pair, the value Python does not need to be supplied separately, and the level-beginner and format-course pairs remain.
Key Takeaways
- A dictionary contains key-value pairs that can be identified through their keys.
- A lookup takes a key and finds its corresponding value.
- The del statement removes a specified key-value pair.
- The deletion form uses the dictionary and the key inside the indexing operator.
- You do not need to know the value corresponding to the key in order to delete the pair.
Key Takeaways
- Dictionary deletion is a key-based operation.
- The del statement receives the dictionary and the indexed key identifying the pair to remove.
- Deleting one pair does not target the other pairs in the dictionary.
- The associated value does not need to be known or supplied separately.