Python Queue: Understanding FIFO & LIFO with Practical Examples
What Is a Queue in Python?
A queue is a data structure that stores items in a linear order. Items are added at the rear and removed from the front, following the First‑In, First‑Out (FIFO) principle. The queue exposes two logical ends: front and rear—enqueue at the rear, dequeue from the front.
In this tutorial you’ll learn:
- How Python’s
queuemodule works. - Types of queues available in Python.
- How to install and import the module.
- Key methods on
QueueandLifoQueueobjects. - Hands‑on examples for FIFO and LIFO queues.
- Batch operations, sorting, and reversing queues.
How Does the Queue Module Work?
The behaviour is analogous to a physical line: the first person in line is the first served. In the queue data structure, the element that is enqueued first will be dequeued first.

The rear point is where new items are inserted (e.g., value 7). The front point is where items leave the queue; removing the first item yields the value 1.
Because the queue is linear, you cannot remove an arbitrary element in the middle; you must dequeue all preceding elements first.

Types of Queues in Python
Python’s queue module offers two primary queue classes:
- FIFO Queue – use
Queue(). The oldest item is always the first to be removed. - LIFO Queue – use
LifoQueue(). The most recently added item is returned first.
Getting Started: Importing the Module
Python ships the queue module by default; no external installation is required.
import queue
Instantiate a FIFO queue:
q1 = queue.Queue()
Instantiate a LIFO queue:
q1 = queue.LifoQueue()
Key Methods
Both Queue and LifoQueue expose the same core API:
put(item)– enqueue an item.get()– dequeue and return the next item.empty()– returnsTrueif the queue has no items.full()– returnsTrueif the queue has reached its maximum size.qsize()– returns the current number of items.
FIFO Queue Example
Enqueue a Single Item
import queue q1 = queue.Queue() q1.put(10) # add 10 to the queue
Enqueue Multiple Items with a Size Limit
q1 = queue.Queue(maxsize=5) # maximum of 5 items
for i in range(1, 6):
q1.put(i)
print(q1.full()) # → True
Output:
True
Dequeue an Item
item = q1.get()
print('Dequeued item:', item)
Output:
Dequeued item: 10
LIFO Queue Example
Enqueue a Single Item
q1 = queue.LifoQueue() q1.put(10)
Dequeue an Item
item = q1.get()
print('Dequeued from LIFO:', item)
Output:
Dequeued from LIFO: 10
Batch Operations
FIFO – Enqueue 20 Numbers
q1 = queue.Queue()
for i in range(20):
q1.put(i)
FIFO – Dequeue All
while not q1.empty():
print('Value:', q1.get())
Output:
Value: 0 Value: 1 ... Value: 19
LIFO – Enqueue 20 Numbers
q1 = queue.LifoQueue()
for i in range(20):
q1.put(i)
LIFO – Dequeue All
while not q1.empty():
print('Value:', q1.get())
Output:
Value: 19 Value: 18 ... Value: 0
Sorting a Queue (Bubble Sort)
q1 = queue.Queue()
for item in [11, 5, 4, 21, 3, 10]:
q1.put(item)
n = q1.qsize()
for i in range(n):
x = q1.get()
for j in range(n-1):
y = q1.get()
if x > y:
q1.put(y)
else:
q1.put(x)
x = y
q1.put(x)
while not q1.empty():
print(q1.queue[0], end=' ')
q1.get()
Output:
3 4 5 10 11 21
Reversing a Queue Using Recursion
def reverse_queue(src, dest):
item = src.get()
if not src.empty():
reverse_queue(src, dest)
dest.put(item)
return dest
q1 = queue.Queue()
for item in [11, 5, 4, 21, 3, 10]:
q1.put(item)
q2 = queue.Queue()
reverse_queue(q1, q2)
while not q2.empty():
print(q2.queue[0], end=' ')
q2.get()
Output:
10 3 21 4 5 11
Quick Summary
- Python’s
queue.Queueimplements FIFO;queue.LifoQueueimplements LIFO. - Use
put()to enqueue andget()to dequeue. - Both classes expose
empty(),full(), andqsize()for state inspection. - You can set a maximum size on a FIFO queue; attempting to
put()beyond that will block until space frees up. - Sorting and reversing are possible with standard algorithms, though the built‑in
dequefromcollectionsoften offers more flexibility.
Python
- Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
- Python len(): A Practical Guide to Measuring Object Lengths
- Understanding Python's Main Function: A Practical Guide to def main()
- Mastering Python’s Yield: Generator vs Return – A Practical Guide
- Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
- Creating ZIP Archives in Python: From Full Directory to Custom File Selection
- Automating Facebook Login with Python and Selenium: A Step‑by‑Step Guide
- Python List index() – How to Find Element Positions with Practical Examples
- Python Calendar Module: Expert Guide with Code Examples
- FIFO vs LIFO: Key Differences, Pros & Cons, and Practical Applications