Start learning free

LESSON 267 OF 410 · HASH TABLE

LRU Cache in Python in Python

Practice

A cache keeps a few recent results close at hand so they are quick to read again. It can only hold a limited number of entries, so when it is full it must throw one away. An LRU (Least Recently Used) cache throws away the entry that was used longest ago. Build a class LRUCache that stores integer keys with integer values.

The class keeps: self.capacity, a dictionary self.map (key to value) and a list self.order (the keys, least recently used first).

Create it: LRUCache(capacity) makes an empty cache that holds at most capacity entries.

Methods and rules:

  • get(key) returns the value stored for key. A hit counts as a use: the key becomes the most recently used. If the key is not in the cache (never stored, or already evicted), return -1 and change nothing.
  • put(key, value) stores the value for key, and the key becomes the most recently used.
    • If the key is already in the cache, replace its value. Nothing is evicted.
    • If the key is new and the cache already holds capacity entries, first evict the least recently used entry (remove it from the map and from order), then store the new one.
  • Keep order sorted by use: the least recently used key at the front, the most recently used at the end. Every use moves the key to the end.

Tools for this language:

  • In the constructor __init__(self, capacity), save the capacity and create self.map = {} and self.order = [].
  • key in self.map tells whether a key is stored; self.map[key] = value stores or replaces a value; del self.map[key] deletes an entry.
  • self.order.remove(key) removes an item by value, self.order.append(key) adds at the end, and self.order.pop(0) removes and returns the first item.

This version walks over the list of keys for each operation, which is fine for learning. Interview solutions reach O(1) with a hash map plus a doubly linked list.

You start with the code that tests your class (write LRUCache above it):

c = LRUCache(2)
c.put(1, 10)
c.put(2, 20)
print("Get 1: " + str(c.get(1)))
c.put(3, 30)
print("Get 2: " + str(c.get(2)))
print("Get 3: " + str(c.get(3)))
print("Miss 9: " + str(c.get(9)))
c.put(1, 11)
print("Update 1: " + str(c.get(1)))
c.put(4, 40)
print("Evicted 3: " + str(c.get(3)))
print("Kept 1: " + str(c.get(1)))
print("Kept 4: " + str(c.get(4)))
s = LRUCache(1)
s.put(1, 100)
s.put(2, 200)
print("Small 1: " + str(s.get(1)))
print("Small 2: " + str(s.get(2)))

Expected output:

Get 1: 10
Get 2: -1
Get 3: 30
Miss 9: -1
Update 1: 11
Evicted 3: -1
Kept 1: 11
Kept 4: 40
Small 1: -1
Small 2: 200

Check it against the rules: Get 2 is -1 because key 2 was the least recently used when key 3 arrived (key 1 had just been read). Updating key 1 to 11 does not evict anything. With capacity 1, the second put evicts the first key.


Two structures work together: the map gives a fast lookup of the value, and order remembers which key was used when. Both must always hold the same keys.

self.order.remove(key) then self.order.append(key) moves a key to the end. self.order.pop(0) gives the least recently used key.

class LRUCache:
    def __init__(self, capacity):
        self.capacity = capacity
        self.map = {}
        self.order = []

    def get(self, key):
        if key not in self.map: return -1
        self.order.remove(key)
        self.order.append(key)
        return self.map[key]

    def put(self, key, value):
        if key in self.map:
            self.order.remove(key)
        elif len(self.map) >= self.capacity:
            evict = self.order.pop(0)
            del self.map[evict]
        self.map[key] = value
        self.order.append(key)

STEP 1 OF 17
    Ctrl + Enter to check

    Lru Cache in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free