pythoncachingdictionarylru

How to limit the size of a dictionary?


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!


Solution

  • Python 2.7 and 3.1 have OrderedDict and there are pure-Python implementations for earlier Pythons.

    from collections import OrderedDict
    
    class LimitedSizeDict(OrderedDict):
        def __init__(self, *args, **kwds):
            self.size_limit = kwds.pop("size_limit", None)
            OrderedDict.__init__(self, *args, **kwds)
            self._check_size_limit()
    
        def __setitem__(self, key, value):
            OrderedDict.__setitem__(self, key, value)
            self._check_size_limit()
    
        def _check_size_limit(self):
            if self.size_limit is not None:
                while len(self) > self.size_limit:
                    self.popitem(last=False)
    

    You would also have to override other methods that can insert items, such as update. The primary use of OrderedDict is so you can control what gets popped easily, otherwise a normal dict would work.