Removing from dictionaries – the pop() method and the del statement
A common use case for a dictionary is as an associative store: we can keep an association between key and value objects. This means that we may be doing any of the CRUD operations on an item in the dictionary:
- Create a new key and value pair
- Retrieve the value associated with a key
- Update the value associated with a key
- Delete the key (and the corresponding value) from the dictionary
We have two common variations on this theme:
- We have the in-memory dictionary,
dict
, and the variations on this theme in thecollections
module. The collection only exists while our program is running. - We also have persistent storage in the
shelve
anddbm
modules. The data collection is a persistent file in the filesystem, with a dict-like mapping interface.
These are similar, while the distinctions between a shelf.Shelf
and dict
object are minor. This allows us to...