As FIFO data structure you could use next (with corresponding complexity):

  • list: append() amortized O(1) and pop(0) O(n)
  • collections.deque - append() O(1) and popleft() O(1)
  • Queue.queue - get() O(1) and put() O(1) and etc. It is suitable for multi-threaded programming and based on collections.deque internally.
Answer from alex_noname on Stack Overflow
🌐
Python
docs.python.org › 3 › library › queue.html
queue — A synchronized queue class
February 23, 2026 - 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › queue-in-python
Queue in Python - GeeksforGeeks
December 11, 2025 - Python’s queue module provides a thread-safe FIFO queue. You can specify a maxsize.
🌐
Python Module of the Week
pymotw.com › 3 › queue › index.html
queue — Thread-Safe FIFO Implementation
January 28, 2017 - The queue module provides a first-in, first-out (FIFO) data structure suitable for multi-threaded programming. It can be used to pass messages or other data between producer and consumer threads safely. Locking is handled for the caller, so many threads can work with the same Queue instance ...
🌐
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 - It follows the first-in, first-out (FIFO) principle, meaning items are added to the end of the queue and removed from the front. Queues provide efficient order processing and are commonly implemented using arrays or linked lists.
🌐
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.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › queue in python with examples
Queue in Python with Examples - Spark By {Examples}
May 31, 2024 - We will discuss Simple Queues, Double-ended Queues, Circular Queues, and Priority Queue. A Simple Queue, also known as a FIFO Queue, is a linear data structure that follows the FIFO principle.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python queue
Python Queue
October 14, 2024 - But, In Python, there are mainly two types of queues that are discussed below: FIFO Queue: FIFO stands for “First In First Out” which means the element that will be inserted first is the element to come out first.
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.4 documentation
>>> from collections import deque >>> queue = deque(["Eric", "John", "Michael"]) >>> queue.append("Terry") # Terry arrives >>> queue.append("Graham") # Graham arrives >>> queue.popleft() # The first to arrive now leaves 'Eric' >>> queue.popleft() # The second to arrive now leaves 'John' >>> queue # Remaining queue in order of arrival deque(['Michael', 'Terry', 'Graham'])
🌐
W3Schools
w3schools.com › python › python_dsa_queues.asp
Queues with Python
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle....
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › stack-queue-python-using-module-queue
Stack and Queue in Python using queue Module - GeeksforGeeks
August 1, 2022 - ... // Initialize queue Syntax: queue.Queue(maxsize) // Insert Element Syntax: Queue.put(data) // Get And remove the element Syntax: Queue.get() Initializes a variable to a maximum size of maxsize. A maxsize of zero '0' means a infinite queue. This ...
🌐
On-systems
on-systems.tech › blog › 126-fifo-queue-in-python
FIFO Queue in Python
September 24, 2022 - import queueimport queue # A FIFO queue means entries will be received in the order they were enqueued q = queue.Queue() for i in range(5): q.put(i) while not q.empty(): print(q.get()) The following shows the execution of this code in the python ...
🌐
Medium
medium.com › @shras_a › queue-in-python-34a74641502e
Queue in Python. Queues are fundamental data structures… | by Shravya | Medium
November 14, 2024 - Queue in Python Queues are fundamental data structures that follows the First In, First Out (FIFO) principle, meaning the first element added to the queue will be the first one to be removed. Think …
🌐
Real Python
realpython.com › queue-in-python
Python Stacks, Queues, and Priority Queues in Practice – Real Python
December 1, 2023 - Restart the Python interpreter and import your class again to see the updated code in action: ... >>> from queues import Queue >>> fifo = Queue("1st", "2nd", "3rd") >>> len(fifo) 3 >>> for element in fifo: ... print(element) ...
🌐
Reddit
reddit.com › r/learnprogramming › question about python queues (fifo) enqueue and dequeue methods
r/learnprogramming on Reddit: Question about Python Queues (FIFO) enqueue and dequeue methods
October 16, 2019 -

So I understand the concept of queues being first in first out (just like standing in a line for something), but I don't really get what the lines where I commented "what does this line do?" do for the queue. This is code for a linked list queue in python provided by my professor so please correct me if I'm wrong.

class Queue :

    def __init__( self ):
        self._qhead = None
        self._qtail = None
        self._count = 0

    def is_empty( self ):
        return self._qhead is None
    
    def __len__( self ):
        return self._count     

# Private storage class for creating the linked list nodes.
class _QueueNode: 
  def __init__( self, item ):
    self.item = item
    self.next = None

Here is the code that I'm having trouble interpreting:

    def enqueue( self, item ): 
        node = _QueueNode( item )
        if self.is_empty() :
            self._qhead = node
        else :
            self._qtail.next = node
       self._qtail = node                # what does this line do?
       self._count += 1

Based on my understanding in the if statement, if nothing is in the queue, then the item will become the head of the linked list / the very first item of the queue. Otherwise, whatever the end / tail of the queue is, the pointer of that node will now point to the new item node. I just can't visualize what the self._qtail = node does to this linked list queue

    def dequeue( self ):
        assert not self.is_empty(), "Cannot dequeue from an empty queue."
        node = self._qhead
        if self._qhead is self._qtail :    # what does this mean
            self._qtail = None
        self._qhead = self._qhead.next            # what happens here?
        self._count -= 1
        return node.item 

Similarly, in the dequeue method it obviously cannot remove anything from an empty linked list so this code will assert an error. Because queues are FIFO, that means the very first element (in a regular list) / the head of the linked list, will be the one that is removed, so I understand the need to set up a node variable to be returned at the end of this method. However, someone please explain to me what the lines of code actually mean. Thanks in advance!

🌐
GitHub
github.com › alex-petrenko › faster-fifo
GitHub - alex-petrenko/faster-fifo: Faster alternative to Python's multiprocessing.Queue (IPC FIFO queue) · GitHub
pip install Cython python setup.py build_ext --inplace pip install -e . from faster_fifo import Queue from queue import Full, Empty q = Queue(1000 * 1000) # specify the size of the circular buffer in the ctor # any pickle-able Python object can be added to the queue py_obj = dict(a=42, b=33, c=(1, 2, 3), d=[1, 2, 3], e='123', f=b'kkk') q.put(py_obj) assert q.qsize() == 1 retrieved = q.get() assert q.empty() assert py_obj == retrieved for i in range(100): try: q.put(py_obj, timeout=0.1) except Full: log.debug('Queue is full!') num_received = 0 while num_received < 100: # get multiple messages at once, returns a list of messages for better performance in many-to-few scenarios # get_many does not guarantee that all max_messages_to_get will be received on the first call, in fact # no such guarantee can be made in multiprocessing systems.
Starred by 207 users
Forked by 33 users
Languages   C++ 82.6% | Python 13.6% | CMake 1.0% | Shell 1.0% | C 0.9% | Starlark 0.6%
🌐
Python Module of the Week
pymotw.com › 2 › Queue
Queue – A thread-safe FIFO implementation - Python Module of the Week
Now available for Python 3! Buy the book! ... The Queue module provides a FIFO implementation suitable for multi-threaded programming. It can be used to pass messages or other data between producer and consumer threads safely. Locking is handled for the caller, so it is simple to have as many ...