How do I hash a built-in node in deque (which is a double linked list) and delete the node in the middle in O(1)? Is the built-in node exposed?
For example, I want to save a deque's node in dict so I can delete the node in constant time later.
This is a use case in LRU, using deque so I don't need to write my own double linked list.
from collections import deque class LRU: def __init__(self): self.nodes = deque() self.key2node = {} def insertThenDelete(self): # insert node = deque.Node('k', 'v') # imagine you can expose deque node here self.nodes.appendleft(node) self.key2node = {'k': node} # delete self.key2node['k'].deleteInDeque() # HERE shold remove the node in DLL! del self.key2node['k'] I know you can do del mydeque[2] to delete by index. but I want to do key2node['k'].deleteInDeque() delete by referance.
dequehere? The class by that name in the built-in collections module does not have aNodeattribute.collections.deque, give up and use something else.collections.dequeis double linked list based (stackoverflow.com/a/6257048/3023116), so you cannot have O(1) deletions from the middle.