From the course: Programming Foundations: Data Structures

Unlock the full course today

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

Use a deque as a queue in Python

Use a deque as a queue in Python - Python Tutorial

From the course: Programming Foundations: Data Structures

Use a deque as a queue in Python

- [Instructor] In Python, we can use a deck or a double-ended queue to implement a queue structure. A deck excels at performing insertions and deletions from both ends, making it perfect for us to use as a queue. To implement FIFO or first in, first out, we'll need to remove items from the front of the deck and add items to the back. For this example, we'll create a deck that stores a printer queue. Since deck is a part of the Python collections module, we'll need to import it first. Then we'll create an empty deck to store the printer queue, to end queue or add items to the queue, we'll use the append method. Let's add a few documents. These will be processed in FIFO order. Let's print out the first document, the Taylor Swift tickets. We can use the pop left method to remove an item from the front of the deck. In fact, we can put this in a loop to print out each document from the queue. We'll use a while loop to continue printing as long as there are items in the queue. The lens…

Contents