'Move an existing element to the end (or beginning if last==False).
Raises KeyError if the element does not exist.
When last=True, acts like a fast version of self[key]=self.pop(key).'

def move_to_end(self, key, last=True):

    link = self.__map[key]
    link_prev = link.prev
    link_next = link.next
    link_prev.next = link_next
    link_next.prev = link_prev
    root = self.__root
    if last:
        last = root.prev
        link.prev = last
        link.next = root
        last.next = root.prev = link
    else:
        first = root.next
        link.prev = root
        link.next = first
        root.next = first.prev = link
