queue | Python Glossary – Real Python

In Python, a queue is a data structure that follows the First In, First Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed.

Queues are useful when you need to maintain the order of elements as they’re processed, similar to a line of people waiting for service.

In Python, you can implement queues using various modules and data structures, including the queue module and the collections.deque class, or even using regular lists.

Example

Here’s a quick example of using a queue in Python with the queue module:

In this example, you add elements to the queue using the .put() method and remove them using the .get() method. The order of removal is the same as the order of insertion, demonstrating the FIFO behavior.

Python Stacks, Queues, and Priority Queues in Practice

For additional information on related topics, take a look at the following resources: