Understanding Queues in Python: Enqueue, Dequeue, and Priority Queues

As I continue my journey through data structures, I recently explored a simple yet powerful concept — the queue. Queues are everywhere in computing, from scheduling processes in an operating system to managing background tasks in web servers.
In this post, I’ll share what I learned about queues, their key operations (enqueue and dequeue), and a brief look at priority queues, all through hands-on Python examples.
🧠 What is a Queue?
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. The element that is inserted first is the one removed first — like people lining up at a counter.
📌 Queue Operations
➕ Enqueue Operation (Insert)
To enqueue means to add an element to the rear (end) of the queue.
"""
To enqueue is to add/insert elements to the rear/back of the queue
----STEPS----
1. Check if the queue is full
2. Insert the element
3. Update the pointer to the current back element
"""
queue = []
queue.append(20)
queue.append(30)
queue.append(40)
print(queue) # Output: [20, 30, 40]
In Python, we use append()
to simulate the enqueue operation. Since Python lists are dynamic, we usually don’t check for "full" unless implementing with a fixed size.
➖ Dequeue Operation (Remove)
To dequeue means to remove an element from the front of the queue.
"""
To dequeue is to remove elements from the front of the queue
----STEPS----
1. Check if the queue is empty
2. Remove the front element
3. Update the pointer to the current front element
"""
queue = [20, 30, 40]
queue.pop(0)
print(queue) # Output: [30, 40]
Here, we use pop(0)
to remove the first element — this simulates removing the front of the queue.
🧱 Queue Types
There are multiple types of queues, but first i focused on one special type, the Priority queue
🔺 Priority Queue
In a priority queue, elements are served based on their priority — not just their arrival order. Think of a hospital ER where critical patients are treated before others.
import heapq
priority_queue = []
heapq.heappush(priority_queue, (1, 'low_priority'))
heapq.heappush(priority_queue, (0, 'high_priority'))
result = heapq.heappop(priority_queue)
print(result) # Output: (0, 'high_priority')
We use Python’s heapq
module which allows us to treat a list as a min-heap (smallest number = highest priority). So, (0, 'high_priority')
is dequeued before (1, 'low_priority')
.
🌍 Real-World Applications of Queues
Understanding queues isn’t just theoretical — they’re used everywhere:
✅ Enqueue/Dequeue Applications:
Print Queue: Jobs sent to a printer are printed in order.
Call Center System: Calls are answered in the order received.
Breadth-First Search (BFS): Used in graph algorithms and AI.
✅ Priority Queue Applications:
Task Scheduling: Operating systems schedule tasks by priority.
Navigation Systems: Dijkstra's algorithm (shortest path).
Event-driven Systems: Handling events based on urgency.
🎯 Final Thoughts
This exercise helped me better understand queues — both conceptually and practically. Writing and running these examples in Python really solidified how enqueueing and dequeueing work under the hood. Learning about priority queues gave me a glimpse of how data can be managed more efficiently in time-critical systems.
Subscribe to my newsletter
Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
