Python offers many useful features in its standard libraries. Hiding in the collections library, there’s a lesser known data structure, the deque [1] (apparently pronounced deck). Let me show you what it is, how it works and why it is a perfect data structure for plotting continuous data from a stream!
Deque is a data type imported from the collections module, it stands for double ended queue. Essentially it is a list with two ends, so you can easily add/remove from the head or tail.
To use it we declare a variable for the queue. Since the queue has two ends, we can easily append to both ends with either append to add to the tail or appendleft to add to the beginning of the list.
from collections import dequemy_queue = deque()
my_queue.append("a")
my_queue.append("b")
my_queue.appendleft("c")
my_queue.appendleft("d")
print(my_queue) # deque(['d', 'c', 'a', 'b'])
You can also pop from both ends of the queue with either pop for the right side or popleft for removing elements from the start.
my_queue.pop()
print(my_queue) # deque(['d', 'c', 'a'])my_queue.popleft()
print(my_queue) # deque(['c', 'a'])