I need to build a circular buffer as a deque in python with efficient search (not O(n) el in deque
, but O(1) like in set())
from collections import deque
deque = deque(maxlen=10) # in my case maxlen=1000
for i in range(20):
deque.append(i)
deque
Out[1]: deque([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
10 in deque # but it takes O(n), I need O(1)
Out[1]: True
I guess I need to maintain a separate dictionary for lookup and remove from it once deque is full, but don't understand how. I don't need to remove from the middle of deque, just to append
as deque did it and quick lookup.