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
๐ŸŒ
O'Reilly
oreilly.com โ€บ library โ€บ view โ€บ python-cookbook โ€บ 0596001673 โ€บ ch17s15.html
Implementing a First-In First-Out Container - Python Cookbook [Book]
July 19, 2002 - class Fifo: def _ _init_ _(self): self.first = None self.last = None def append(self, data): node = [data, None] # [payload, 'pointer'] "pair" if self.first is None: self.first = node else: self.last[1] = node self.last = node def pop(self): if self.first is None : raise IndexError node = self.first self.first = node[1] return node[0] if _ _name_ _=='_ _main_ _': # Run a test/example when run as a script: a = Fifo( ) a.append(10) a.append(20) print a.pop( ) a.append(5) print a.pop( ) print a.pop ()
Authors ย  Alex MartelliDavid Ascher
Published ย  2002
Pages ย  608
๐ŸŒ
GitHub
github.com โ€บ hbock โ€บ byte-fifo
GitHub - hbock/byte-fifo: Small byte-oriented FIFO for Python. Compatible with Python 2.6+ and 3.
Small byte-oriented FIFO for Python. Compatible with Python 2.6+ and 3. - hbock/byte-fifo
Starred by 11 users
Forked by 5 users
Languages ย  Python 100.0% | Python 100.0%
๐ŸŒ
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. Queues are widely used in real-life scenarios, like ticket booking, or CPU task scheduling, where ...
๐ŸŒ
GitHub
gist.github.com โ€บ egorps โ€บ 7695667
Python FIFO example ยท GitHub
Save egorps/7695667 to your computer and use it in GitHub Desktop. Download ZIP ยท Python FIFO example ยท Raw ยท FIFO ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python queue: fifo, lifo example
Python Queue: FIFO, LIFO Example
August 12, 2024 - The module is available by default with python, and you donโ€™t need any additional installation to start working with the queue. There are 2 types of queue FIFO (first in first out) and LIFO (last in first out).
Find elsewhere
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ data-structures-and-algorithms โ€บ python-data-structure-exercise-29.php
Python Data Structure: Create a FIFO queue - w3resource
July 28, 2025 - Write a Python program to create a FIFO queue. Sample Solution: Python Code: import queue q = queue.Queue() #insert items at the end of the queue for x in range(4): q.put(str(x)) #remove items from the head of the queue while not q.empty(): print(q.get(), end=" ") print("\n") Sample Output: 0 1 2 3 ยท
๐ŸŒ
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 ...
Top answer
1 of 3
33

Apart from learning purposes I would not advise using a custom data structure for making a LIFO or FIFO. The built in data-type list is just fine after all.

You can add items using the append method and remove them using pop. For a LIFO this would look like this:

stack = list()
stack.append(1)
stack.append(2)
stack.append(3)

print stack.pop()  #3
print stack.pop()  #2
print stack.pop()  #1

If you supply an integer argument for pop you can specify which element to remove. For a FIFO use the index 0 for the first element:

stack = list()
stack.append(1)
stack.append(2)
stack.append(3)

print stack.pop(0)  #1
print stack.pop(0)  #2
print stack.pop(0)  #3
2 of 3
1

Well, seeing as your class is probably over now and you didn't mention your class (or that it had to be a linked list) in the question itself, I'll just tell you the built-in easy way to do it, for now, which is probably more pertinent to your current situation (and will help people who find your question).

import sys;
if sys.version_info[0]>2: #Just making sure the program works with both Python 2.x and 3.x
    from queue import Queue
else:
    from Queue import Queue

q=Queue()
q.put("first") #Put an item on the Queue.
q.put("second")
q.put("third")

while not q.empty(): #If it's empty, the program will stall if you try to get from it (that's why we're checking)
    print(q.get()) #Get an item from the Queue

This outputs

first
second
third

Really, though, I'm not sure what advantages this has over Constantinius's answer, but since it's an included module, I would think there must be an advantage somewhere. I know they are used with threads from the threading module. There are more functions associated with Queues than I've mentioned here.

To learn more, open your Python interpreter and type this:

from queue import Queue #or from Queue import Queue for 2.x
help(Queue) #Press q to exit the help

Don't ask me what blocking is, but this may use the term how it's used in the Queue class documentation: http://en.wikipedia.org/wiki/Blocking_(computing)

๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ Queue
Queue โ€“ A thread-safe FIFO implementation - Python Module of the Week
Some of the features described here may not be available in earlier versions of Python. If you are looking for examples that work under Python 3, please refer to the PyMOTW-3 section of the site. Now available for Python 3! Buy the book! ... The Queue module provides a FIFO implementation suitable for multi-threaded programming.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-os-mkfifo-method
Python os.mkfifo() method - GeeksforGeeks
July 12, 2025 - OS module comes under Pythonโ€™s standard utility modules. os.mkfifo() method is used to create a FIFO (a named pipe) named path with the specified mode. FIFOs are named pipe which can be accessed like other regular files.
๐ŸŒ
Medium
medium.com โ€บ dm03514-tech-blog โ€บ python-algorithms-implementing-a-fifo-queue-using-a-linked-list-57cf700a6395
Python Algorithms โ€” Implementing a FIFO Queue Using a Linked List | by dm03514 | Dm03514 Tech Blog | Medium
August 29, 2021 - This is referred to as a โ€œfirst in first out queueโ€ (FIFO). This post details an implementation of a FIFO queue using a linked list. This post hopes to be a useful algorithm interview study reference. All code is available on github at dm03514/python-algorithms.
๐ŸŒ
Medium
medium.com โ€บ @khasnobis.sanjit890 โ€บ linear-data-structure-queue-in-python-fifo-first-in-first-out-bbd03f6b3b0f
Linear Data Structure Queue in python โ€” FIFO โ€” First in First Out | by Sanjit Khasnobis | Medium
September 16, 2023 - This ordering principle is sometimes called FIFO, first-in first-out. It is also known as โ€œfirst-come first-served. The best example of a queue is the typical line that we all participate when we stand in the payment queue of supermarket or ...
๐ŸŒ
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 - To delete the element in the queue the โ€œ get() โ€ function is used. It is known as the dequeue operation. The Python queue works on the FIFO ( First In First Out ) principle i.e.
๐ŸŒ
AWS
aws.amazon.com โ€บ blogs โ€บ developer โ€บ using-python-and-amazon-sqs-fifo-queues-to-preserve-message-sequencing
Using Python and Amazon SQS FIFO Queues to Preserve Message Sequencing | AWS Developer Tools Blog
January 16, 2021 - This example is using boto3, the AWS SDK for Python. This first script sends an XML message to a queue named TradeStatus.fifo, and the second script receives the message from the same queue. Messages can contain up to 256 KB of text in any format. Any component can later retrieve the messages programmatically using the Amazon SQS API.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_os_mkfifo.asp
Python os.mkfifo() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... #Import os Library import os, sys # Create a FIFO named path with the mode specified os.mkfifo('/tmp/test', 0o644)