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!
Here's a simple, no-LRU Python 2.6+ solution (in older Pythons you could do something similar with
UserDict.DictMixin
, but in 2.6 and better that's not recommended, and the ABCs fromcollections
are preferable anyway...):As other answers mentioned, you probably don't want to subclass dict -- the explicit delegation to
self.d
is unfortunately boilerplatey but it does guarantee that every other method is properly supplied bycollections.MutableMapping
.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 ofOrderedDict
is so you can control what gets popped easily, otherwise a normaldict
would work.Here is a simple and efficient LRU cache written with dirt simple Python code that runs on any python version 1.5.2 or later:
There have been many good answers, but I want to point out a simple, pythonic implementation for LRU cache. It's similar to Alex Martelli's answer.
You can create a custom dictionary class by subclassing dict. In your case, you would have to override
__setitem__
to have check your own length and delete something if the limit is recahed. The following example would print the current lenght after every insertion:cachetools will provide you nice implementation of Mapping Hashes that does this (and it works on python 2 and 3).
Excerpt of the documentation: