From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Use deque as a queue

Use deque as a queue

- [Instructor] In Python, we can use a deque, or a double-ended queue, to store our data in a queue structure. A deque excels at performing insertion and deletion operations from both ends. This is great for us to use it as a queue. We need to be able to remove items from the front and add items to the back to achieve FIFO functionality. For this example, we'll create a deque that stores a printer queue. Deque lives in the collections module. So we'll need to import it from there, then we'll create an empty deque to store our printer queue. To in-queue or add items to the queue, we'll use the append method. Let's append a few documents. These will be processed in first in, first out order. Let's print out the Taylor Swift tickets. We can use the popleft method to remove an item from the front of the queue. Let's print it out. In fact, we can put this in a loop to print out each document in FIFO order. We'll use a…

Contents