Dictionary Basics and Operations
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.
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
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"].
- 1Identify the dictionary
records is the dictionary that will be changed.
- 2Read the key
The key inside the indexing operator is status.
- 3Locate the pair
The key status corresponds to the value active, so the targeted pair is status with active.
- 4Apply del
The del statement removes that key-value pair. The value active does not need to be supplied separately.
- 5Inspect 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 |
Dictionary deletion is described as a key-based operation.
Mistakes to Avoid
Common mistake Trying to delete by writing the value instead of the key.
What it looks likeThe pair is {"status": "active"}, but the deletion target is status, not active.
Why it failsThe deletion form identifies the pair through the key inside the indexing operator.
Do this insteadUse the dictionary and the key that identifies the pair.
Common mistake Assuming the entire dictionary is removed.
What it looks likeDeleting the status pair from {"status": "active", "owner": "Mina"}.
Why it failsThe operation targets one specified key-value pair.
Do this insteadExpect the other key-value pairs to remain.
Common mistake Believing that the corresponding value must be known first.
What it looks likeTrying to write both status and active as deletion inputs.
Why it failsThe source description explicitly states that the value corresponding to the key is not needed for this operation.
Do this insteadIdentify 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
- 1A dictionary contains key-value pairs that can be identified through their keys.
- 2A lookup takes a key and finds its corresponding value.
- 3The del statement removes a specified key-value pair.
- 4The deletion form uses the dictionary and the key inside the indexing operator.
- 5You do not need to know the value corresponding to the key in order to delete the pair.
At a glance
- 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.