When the
pop()method of a list is used [...] in classQueueit returns the 1st element.
No, it does not. It still returns the last element of the list. You should not look at dequeue and pop, you should instead concentrate on the Queue.enqueue and Stack.push methods.
The Stack class appends to the items list, so puts the new element at the end:
def push(self, item):
self.items.append(item)
while the Queue inserts at the start of the items list:
def enqueue(self, item):
self.items.insert(0,item)
So when you pop from the stack, you remove the element that was last added. When you pop from the queue, you remove the element that was first added, as everything else was inserted in front of it.
Put differently, the Queue hold items in chronological order (newest item is at the start of the list), while the Stack holds them in reverse chronological order (newest item is at the end of the list). Popping still removes the last element from the list.
Best way to continuously pop elements from a queue until condition is reached
data structures - Why does, in python, Stack's pop method and Queue's dequeue method behave differently when both have same code? - Stack Overflow
Implementing an efficient queue in Python - Stack Overflow
python - Popping items from queue does not delete all of them - Stack Overflow
Videos
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.
As Uri Goren astutely noted above, the Python stdlib already implemented an efficient queue on your fortunate behalf: collections.deque.
What Not to Do
Avoid reinventing the wheel by hand-rolling your own:
- Linked list implementation. While doing so reduces the worst-case time complexity of your
dequeue()andenqueue()methods to O(1), thecollections.dequetype already does so. It's also thread-safe and presumably more space and time efficient, given its C-based heritage. - Python list implementation. As I note below, implementing the
enqueue()methods in terms of a Python list increases its worst-case time complexity to O(n). Since removing the last item from a C-based array and hence Python list is a constant-time operation, implementing thedequeue()method in terms of a Python list retains the same worst-case time complexity of O(1). But who cares?enqueue()remains pitifully slow.
To quote the official deque documentation:
Though
listobjects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs forpop(0)andinsert(0, v)operations which change both the size and position of the underlying data representation.
More critically, deque also provides out-of-the-box support for a maximum length via the maxlen parameter passed at initialization time, obviating the need for manual attempts to limit the queue size (which inevitably breaks thread safety due to race conditions implicit in if conditionals).
What to Do
Instead, implement your Queue class in terms of the standard collections.deque type as follows:
from collections import deque
class Queue:
'''
Thread-safe, memory-efficient, maximally-sized queue supporting queueing and
dequeueing in worst-case O(1) time.
'''
def __init__(self, max_size = 10):
'''
Initialize this queue to the empty queue.
Parameters
----------
max_size : int
Maximum number of items contained in this queue. Defaults to 10.
'''
self._queue = deque(maxlen=max_size)
def enqueue(self, item):
'''
Queues the passed item (i.e., pushes this item onto the tail of this
queue).
If this queue is already full, the item at the head of this queue
is silently removed from this queue *before* the passed item is
queued.
'''
self._queue.append(item)
def dequeue(self):
'''
Dequeues (i.e., removes) the item at the head of this queue *and*
returns this item.
Raises
----------
IndexError
If this queue is empty.
'''
return self._queue.pop()
The proof is in the hellish pudding:
>>> queue = Queue()
>>> queue.enqueue('Maiden in Black')
>>> queue.enqueue('Maneater')
>>> queue.enqueue('Maiden Astraea')
>>> queue.enqueue('Flamelurker')
>>> print(queue.dequeue())
Flamelurker
>>> print(queue.dequeue())
Maiden Astraea
>>> print(queue.dequeue())
Maneater
>>> print(queue.dequeue())
Maiden in Black
It Is Dangerous to Go Alone
Actually, don't do that either.
You're better off just using a raw deque object rather than attempting to manually encapsulate that object in a Queue wrapper. The Queue class defined above is given only as a trivial demonstration of the general-purpose utility of the deque API.
The deque class provides significantly more features, including:
...iteration, pickling,
len(d),reversed(d),copy.copy(d),copy.deepcopy(d), membership testing with the in operator, and subscript references such asd[-1].
Just use deque anywhere a single- or double-ended queue is required. That is all.
You can keep head and tail node instead of a queue list in queue class
class Node:
def __init__(self, item = None):
self.item = item
self.next = None
self.previous = None
class Queue:
def __init__(self):
self.length = 0
self.head = None
self.tail = None
def enqueue(self, value):
newNode = Node(value)
if self.head is None:
self.head = self.tail = newNode
else:
self.tail.next = newNode
newNode.previous = self.tail
self.tail = newNode
self.length += 1
def dequeue(self):
item = self.head.item
self.head = self.head.next
self.length -= 1
if self.length == 0:
self.tail = None
return item