๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dsa_queues.asp
Queues with Python
Since Python lists has good support for functionality needed to implement queues, we start with creating a queue and do queue operations with just a few lines:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ queue-in-python
Queue in Python - GeeksforGeeks
May 29, 2026 - Explanation: queue.Queue class handles thread-safe operations.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ queue.html
queue โ€” A synchronized queue class
The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO queue, the first tasks added are the first retrieved. In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack).
๐ŸŒ
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 - This is analogous to a physical queue of people waiting in line โ€” the first person in line is the first to be served. The main operations of a queue are: Enqueue โ€” Add an element to the end of the queue ยท
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-implement-a-queue-in-python
How to implement a queue in Python
Python implements queues using lists, collections.deque for efficient operations, and the queue module for thread-safe FIFO queues.
๐ŸŒ
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
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ python queue
Python Queue
October 14, 2024 - Dequeue: In this type of operation, we can delete the element from any end of the queue such as front or rear. Also, the insertion of elements takes place at both ends. The time complexity for this operation is the same as other operations i.e. O(1). There are some important methods available in Python which are very useful.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ queue
queue | Python Standard Library โ€“ Real Python
Language: Python ยท >>> import queue >>> tasks = queue.Queue() >>> tasks.put("task1") >>> tasks.put("task2") >>> tasks.get() 'task1' >>> tasks.get() 'task2' Provides thread-safe FIFO, LIFO, and priority queues ยท Supports blocking and non-blocking operations ยท
Find elsewhere
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ queue in python: an in-depth guide
Queue in Python: An In-Depth Guide
August 12, 2024 - Use queue.Queue for thread-safe operations. Q4. Can a queue be used for sorting? A. A priority queue can be used for sorting elements based on priority. Q5. What are some real-world examples of queues? A. Examples include customer service lines, print job management, and request handling in web servers. ... My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more.
๐ŸŒ
Programiz
programiz.com โ€บ dsa โ€บ queue
Queue Data Structure and Implementation in Java, Python and C/C++
The complexity of enqueue and dequeue operations in a queue using an array is O(1). If you use pop(N) in python code, then the complexity might be O(n) depending on the position of the item to be popped.
๐ŸŒ
Real Python
realpython.com โ€บ queue-in-python
Python Stacks, Queues, and Priority Queues in Practice โ€“ Real Python
December 1, 2023 - In this section, youโ€™ll learn ... At the very least, every queue provides operations for adding and removing elements in constant time or O(1) using the Big O notation....
๐ŸŒ
Llego
llego.dev โ€บ home โ€บ blog โ€บ implementing a queue in python: a step-by-step guide
Implementing a Queue in Python: A Step-by-Step Guide - llego.dev
August 5, 2023 - Queues are useful when the order of operations matters, like: ... In Python, queues can be implemented using lists or the collections.deque object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ implementation-of-queue-using-list-in-python
Implementation of Queue using List in Python - GeeksforGeeks
July 23, 2025 - Time Complexity: O(1) for Enqueue (element insertion in the queue) as we simply increment pointer and put value in array, O(n) for Dequeue (element removing from the queue). Auxiliary Space: O(n), as here we are using an n size array for implementing Queue ยท We can notice that the Dequeue operation is O(n) which is not acceptable.
๐ŸŒ
W3Schools
w3schools.com โ€บ dsa โ€บ dsa_data_queues.php
DSA Queues
Since Python lists has good support for functionality needed to implement queues, we start with creating a queue and do queue operations with just a few lines:
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
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ guide-to-queues-in-python
Guide to Queues in Python
April 18, 2024 - The queue module in Python's standard library provides a more specialized approach to queue management, catering to various use cases: ... Note: Opt for the queue module, which is designed to be thread-safe. This ensures that concurrent operations on the queue do not lead to unpredictable outcomes.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ asyncio-queue.html
Queues โ€” Python 3.14.6 documentation
Note that methods of asyncio queues donโ€™t have a timeout parameter; use asyncio.wait_for() function to do queue operations with a timeout.
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ queue-in-python
Queue in Python | Board Infinity
August 9, 2025 - Python's built-in module Queue is used to implement queues. queue. Queue(maxsize) sets a variable's initial value to maxsize, its maximum size.