def __setitem__(self, k, v):
    if k not in self.d:
        self.L.append(k)
self.sorted = False
self.d[k] = v

def __delitem__(self, k):
    del self.d[k]
self.L.remove(k)

def __iter__(self):
    if not self.sorted:
        self.L.sort()
self.sorted = True
return iter(self.L)
Key-ordered dict in Python