Is popleft() faster than pop(0) ?
Yes. list.pop(0) is O(n), and deque.popleft() is O(1).
How is python BFS O(E + V). Doesn’t dequeuing using queue.pop(0) require O(n?
I was surprised at how slow list.pop() is! And list.remove() is even many times slower
I have a deque that consists of datetime objects (the rightmost elements will always be newer) and I'm trying to write some logic to pop items older than n seconds off the queue. I have it sort of functioning, but my while loop depends on looking at the -1 element of the queue, which doesn't always exist (in the case where it has been emptied because all items in the queue were older than the threshold). I can get around it with some try/except stuff or more conditionals, but none of that seems very pythonic.
while True:
targettime = datetime.now() - timedelta(seconds=5 * 60)
while queue[-1] >= targettime and len(queue) > 0:
queue.pop()
if len(queue) == 0:
do_work()
time.sleep(60)Any advice on how to handle this better? I'm open to entirely new solutions as well, using a deque seemed like the best approach but now I'm not so sure.