Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

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 rearenqueue at the rear, dequeue from the front.

In this tutorial you’ll learn:

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.

Python Queue: Understanding FIFO & LIFO with Practical Examples

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.

Python Queue: Understanding FIFO & LIFO with Practical Examples

Types of Queues in Python

Python’s queue module offers two primary queue classes:

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:

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

  1. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  2. Python len(): A Practical Guide to Measuring Object Lengths
  3. Understanding Python's Main Function: A Practical Guide to def main()
  4. Mastering Python’s Yield: Generator vs Return – A Practical Guide
  5. Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
  6. Creating ZIP Archives in Python: From Full Directory to Custom File Selection
  7. Automating Facebook Login with Python and Selenium: A Step‑by‑Step Guide
  8. Python List index() – How to Find Element Positions with Practical Examples
  9. Python Calendar Module: Expert Guide with Code Examples
  10. FIFO vs LIFO: Key Differences, Pros & Cons, and Practical Applications