queue
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:
>>> import queue
>>> q = queue.Queue()
>>> q.put("first")
>>> q.put("second")
>>> q.put("third")
>>> q
<queue.Queue object at 0x1040091d0>
>>> q.get()
'first'
>>> q.get()
'second'
>>> q.get()
'third'
>>> q.empty()
True
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.
Related Resources
Tutorial
Python Stacks, Queues, and Priority Queues in Practice
In this tutorial, you'll take a deep dive into the theory and practice of queues in programming. Along the way, you'll get to know the different types of queues, implement them, and then learn about the higher-level queues in Python's standard library. Be prepared to do a lot of coding.
For additional information on related topics, take a look at the following resources:
By Leodanis Pozo Ramos • Updated June 24, 2025