๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
February 23, 2026 - Source code: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multip...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ queue-in-python
Queue in Python - GeeksforGeeks
December 11, 2025 - Queue is a linear data structure that stores items in a First In First Out (FIFO) manner. The item that is added first will be removed first.
๐ŸŒ
W3Schools
w3schools.com โ€บ PYTHON โ€บ python_dsa_queues.asp
Queues with Python
Python Examples Python Compiler ... Python Certificate Python Training ... A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle....
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ asyncio-queue.html
Queues โ€” Python 3.14.4 documentation
February 22, 2026 - Source code: Lib/asyncio/queues.py asyncio queues are designed to be similar to classes of the queue module. Although asyncio queues are not thread-safe, they are designed to be used specifically i...
๐ŸŒ
Medium
basillica.medium.com โ€บ working-with-queues-in-python-a-complete-guide-aa112d310542
Working with Queues in Python โ€” A Complete Guide | by Basillica | Medium
March 27, 2024 - Queues are a useful data structure in programming that allow you to add and remove elements in a first in, first out (FIFO) order. Python provides a built-in module called queue that implements different types of queue data structures.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ python queue
Python Queue
October 14, 2024 - It will process the tasks in a manner called โ€œFirst In First Outโ€ which means the first process or task in the queue will be executed and removed first, after that other processes will be started. A queue can be implemented in programming languages such as Python, Java, C++, etc.
๐ŸŒ
BitDegree
bitdegree.org โ€บ learn โ€บ python-queue
The Four Types of Python Queue: Definitions and Examples
February 19, 2020 - TL;DR โ€“ A Python queue is a linear data structure, similar to a stack.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ queue
queue | Python Standard Library โ€“ Real Python
The Python queue module provides reliable thread-safe implementations of the queue data structure.
Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - 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.
๐ŸŒ
RQ
python-rq.org
RQ: Simple job queues for Python
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers. It is backed by Redis/Valkey and is designed to have a low barrier to entry.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ python programming for beginners โ€“ free python tutorials โ€บ python queue tutorial: how to implement and use python queue
Python Queue Tutorial: How To Implement And Use Python Queue
April 1, 2025 - In the above program, we create a demo queue and add the elements. After the insertion of elements, we delete all the elements from the queue. ... Python supports the various methods of Queue, that are most commonly used while working with the queue data structure.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-implement-a-queue-in-python
How to implement a queue in Python
Python offers multiple ways to implement queues, each suited to specific needs: lists for simplicity, the Queue module for thread-safe operations in concurrent programming, and the collections.deque module for efficient enqueue and dequeue operations.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ guide-to-queues-in-python
Guide to Queues in Python
April 18, 2024 - The first person in line gets served first, and new arrivals join at the end. This is a real-life example of a queue in action! For developers, especially in Python, queues aren't just theoretical constructs from a computer science textbook. They form the underlying architecture in many applications.
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ queue in python: working with queue data structure in python
Queue in Python: Working With Queue Data Structure in Python
March 5, 2026 - A queue is a built-in module of python used in threaded programming. It stores items sequentially in a FIFO manner. Learn all about the queue in python now!
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Top answer
1 of 8
41

As Uri Goren astutely noted above, the Python stdlib already implemented an efficient queue on your fortunate behalf: collections.deque.

What Not to Do

Avoid reinventing the wheel by hand-rolling your own:

  • Linked list implementation. While doing so reduces the worst-case time complexity of your dequeue() and enqueue() methods to O(1), the collections.deque type already does so. It's also thread-safe and presumably more space and time efficient, given its C-based heritage.
  • Python list implementation. As I note below, implementing the enqueue() methods in terms of a Python list increases its worst-case time complexity to O(n). Since removing the last item from a C-based array and hence Python list is a constant-time operation, implementing the dequeue() method in terms of a Python list retains the same worst-case time complexity of O(1). But who cares? enqueue() remains pitifully slow.

To quote the official deque documentation:

Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation.

More critically, deque also provides out-of-the-box support for a maximum length via the maxlen parameter passed at initialization time, obviating the need for manual attempts to limit the queue size (which inevitably breaks thread safety due to race conditions implicit in if conditionals).

What to Do

Instead, implement your Queue class in terms of the standard collections.deque type as follows:

from collections import deque

class Queue:
    '''
    Thread-safe, memory-efficient, maximally-sized queue supporting queueing and
    dequeueing in worst-case O(1) time.
    '''


    def __init__(self, max_size = 10):
        '''
        Initialize this queue to the empty queue.

        Parameters
        ----------
        max_size : int
            Maximum number of items contained in this queue. Defaults to 10.
        '''

        self._queue = deque(maxlen=max_size)


    def enqueue(self, item):
        '''
        Queues the passed item (i.e., pushes this item onto the tail of this
        queue).

        If this queue is already full, the item at the head of this queue
        is silently removed from this queue *before* the passed item is
        queued.
        '''

        self._queue.append(item)


    def dequeue(self):
        '''
        Dequeues (i.e., removes) the item at the head of this queue *and*
        returns this item.

        Raises
        ----------
        IndexError
            If this queue is empty.
        '''

        return self._queue.pop()

The proof is in the hellish pudding:

>>> queue = Queue()
>>> queue.enqueue('Maiden in Black')
>>> queue.enqueue('Maneater')
>>> queue.enqueue('Maiden Astraea')
>>> queue.enqueue('Flamelurker')
>>> print(queue.dequeue())
Flamelurker
>>> print(queue.dequeue())
Maiden Astraea
>>> print(queue.dequeue())
Maneater
>>> print(queue.dequeue())
Maiden in Black

It Is Dangerous to Go Alone

Actually, don't do that either.

You're better off just using a raw deque object rather than attempting to manually encapsulate that object in a Queue wrapper. The Queue class defined above is given only as a trivial demonstration of the general-purpose utility of the deque API.

The deque class provides significantly more features, including:

...iteration, pickling, len(d), reversed(d), copy.copy(d), copy.deepcopy(d), membership testing with the in operator, and subscript references such as d[-1].

Just use deque anywhere a single- or double-ended queue is required. That is all.

2 of 8
10

You can keep head and tail node instead of a queue list in queue class

class Node:
    def __init__(self, item = None):
        self.item = item
        self.next = None
        self.previous = None


class Queue:
    def __init__(self):
        self.length = 0
        self.head = None
        self.tail = None

    def enqueue(self, value):
        newNode = Node(value)
        if self.head is None:
            self.head = self.tail = newNode
        else:
            self.tail.next = newNode
            newNode.previous = self.tail
            self.tail = newNode
        self.length += 1

    def dequeue(self):
        item = self.head.item
        self.head = self.head.next 
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return item
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ python_data_structure โ€บ python queue data structure
Python Data Structure - Queue
February 13, 2026 - We are familiar with queue in our day to day life as we wait for a service. The queue data structure aslo means the same where the data elements are arranged in a queue. The uniqueness of queue lies in the way items are added and removed. The items are allowed at on end but removed form the other end.
๐ŸŒ
GitHub
github.com โ€บ python โ€บ cpython โ€บ blob โ€บ main โ€บ Lib โ€บ queue.py
cpython/Lib/queue.py at main ยท python/cpython
'''Simple, unbounded FIFO queue. ยท This pure Python implementation is not reentrant. ''' # Note: while this pure Python version provides fairness ยท # (by using a threading.Semaphore which is itself fair, being based ยท # on threading.Condition), fairness is not part of the API contract.
Author ย  python
๐ŸŒ
Troy Fawkes
troyfawkes.com โ€บ home โ€บ blog โ€บ the basics of python multithreading and queues
The Basics of Python Multithreading and Queues - Troy Fawkes
May 13, 2024 - The second visual difference is the task_done() bit at the end. That tells the queue that not only have I retrieved the information from the list, but Iโ€™ve finished with it. If I donโ€™t call task_done() then I run into trouble in threading.
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ queue in python โ€“ implementation explained
Queue in Python: How to Implement Queue in Python
October 14, 2025 - Now, if you want to know why Python is the preferred language for data science, you can go through this blog on Python Data Science tutorial. Queue is a collection of similar items arranged in a linear order.