I’d like to work with a dict in python, but limit the number of key/value pairs to X. In other words, if the dict is currently storing X key/value pairs and I perform an insertion, I would like one of the existing pairs to be dropped. It would be nice if it was the least recently inserted/accesses key but that’s not completely necessary.
If this exists in the standard library please save me some time and point it out!
Python 2.7 and 3.1 have OrderedDict and there are pure-Python implementations for earlier Pythons.
You would also have to override other methods that can insert items, such as
update. The primary use ofOrderedDictis so you can control what gets popped easily, otherwise a normaldictwould work.