Using collections.deque for Queues and Stacks in Python

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

Hi, where is your linked list video you said next coming?

kevinshao
Автор

from collections import deque

q = deque()
q.append("t")
q.append("a")
q.appendleft("z")
q.pop()
print(q)

kvelez
Автор

The terminology is a bit confusing.. if appendleft appends left then append would append right. Yet in the Mary, John example, each call to Append is placing the person to the right.

And if popleft always gets the last item placed, then it seems to make it a FIFO you must use append (right), and to make a LIFO you appendleft? Alternatively, FIFO can be implemented by append and popleft.

How does the deque from collections compare with the official queue.Queue ? or between queue.Queue and multiprocessing.Queue?

For example, I understand queue.Queue and mp.Queue are good for multi-threaded applications, but do they offer better performance/ease-of-use in single-thread applications?

I'm interested in putting/appending very large objects from own custom class into a queue/list. Each object consists of large byte arrays.

BTW, according to willwill100, "Queue is used when communication between processes is needed; Queue.Queue is used when communication between threads is needed in the same process; and collections.deque is usually used as a data structure in the same thread. Queue.Queue is intended for allowing different threads to communicate using queued messages/data, whereas collections.deque is simply intended as a datastructure. That's why Queue.Queue has methods like put_nowait(), get_nowait(), and join(), whereas collections.deque doesn't. Queue.Queue isn't intended to be used as a collection, which is why it lacks the likes of the in operator. if you have multiple threads and you want them to be able to communicate without the need for locks, you're looking for Queue.Queue; if you just want a queue or a double-ended queue as a datastructure, use collections.deque."

bennguyen
Автор

This is not a good video, Your channel puts out great content but is is matching your standards

For the last example with webpage history- you could a stack was simply effective with append() and pop() methods
Although it works, it got confusing with appendleft() and popleft() methods

mazharmumbaiwala