Queue in Python
A queue in Python is a fundamental data structure that follows the FIFO (First In, First Out) principle. This means the element added first is removed first. A queue works much like a real-life waiting line, where the person who arrives first gets served first.
Queues are commonly used in computer science applications such as task scheduling, operating systems, printer management, buffering, and multiprocessing. Python provides several ways to create and manage queues, including lists, queue.Queue, collections.deque, and multiprocessing.Queue.
Table of Contents

Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
What is a Queue in Python?
A queue is a linear data structure in which elements are added from one end, called the rear, and removed from the other end, called the front.
The main idea behind a queue is FIFO (First In, First Out). For example, suppose 20 students send documents to a single printer. The first print request should be processed before the next request. This is a practical example of a queue.
Basic Queue Operations
A queue mainly performs the following operations:
- Enqueue: Adds an element to the queue.
- Dequeue: Removes an element from the front of the queue.
- Front: Accesses the first element.
- Rear: Refers to the last element.
- Size: Returns the number of elements in the queue.
Implementing a Queue in Python
There are multiple ways to implement a queue in Python. Let’s look at the commonly used approaches.
1. Using a Python List
A Python list can be used to create a simple queue. The append() method adds elements, while pop(0) removes the first element.
# Implementing a Queue using a List
queue = []
queue.append("Apple")
queue.append("Mango")
queue.append("Papaya")
print(queue)
# Dequeue operation
print(queue.pop(0))
Output:
['Apple', 'Mango', 'Papaya']
Apple
Although this method is simple, pop(0) is not efficient for large queues because the remaining elements have to be shifted. Its time complexity is O(n).
2. Creating a Queue Using a Class
We can also create our own Queue class. This approach helps us understand how queue operations work internally.
Enqueue Operation
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, value):
self.queue.insert(0, value)
def size(self):
return len(self.queue)
q = Queue()
q.enqueue("Apple")
q.enqueue("Mango")
print("Queue Size:", q.size())
Output:
Queue Size: 2
Dequeue Operation
We can add a dequeue() method to remove the oldest element from the queue.
class Queue:
def __init__(self):
self.queue = []
def enqueue(self, value):
self.queue.insert(0, value)
def dequeue(self):
if len(self.queue) > 0:
return self.queue.pop()
return "Queue is Empty"
q = Queue()
q.enqueue("January")
q.enqueue("February")
print(q.dequeue())
Output:
January
3. Using Python’s queue Module
Python provides a built-in queue module for working with queues. The Queue class is particularly useful when working with multiple threads because it provides thread-safe queue operations.
from queue import Queue
q = Queue()
q.put("Apple")
q.put("Mango")
print(q.get())
Output:
Apple
Important Queue Methods
| Method | Purpose |
|---|---|
put(item) | Adds an item to the queue. |
get() | Removes and returns an item from the queue. |
qsize() | Returns the approximate queue size. |
empty() | Checks whether the queue is empty. |
full() | Checks whether the queue is full. |
4. Using collections.deque
The deque class from Python’s collections module is an efficient choice for implementing a general-purpose queue.
from collections import deque
q = deque()
q.append("Apple")
q.append("Mango")
print(q.popleft())
Output:
Apple
The append() method adds an item to the rear, while popleft() removes an item from the front. These operations are efficient and generally take O(1) time.
5. Using multiprocessing.Queue
multiprocessing.Queue can be used when communication between different processes is required. It allows processes to exchange data through a queue.
from multiprocessing import Queue
q = Queue()
q.put("Apple")
q.put("Mango")
print(q.get())
Output:
Apple
Priority Queue in Python
A priority queue is different from a normal FIFO queue because elements are processed according to their priority. Python’s heapq module can be used to implement a priority queue.
import heapq
pq = []
heapq.heappush(pq, (1, "Low Priority"))
heapq.heappush(pq, (3, "High Priority"))
print(heapq.heappop(pq))
Output:
(1, 'Low Priority')
In this example, the element with priority 1 is removed first.
Which Queue Implementation Should You Use?
| Implementation | Suitable For |
|---|---|
| Python List | Simple examples and learning |
queue.Queue | Thread-safe applications |
collections.deque | Efficient general-purpose queues |
multiprocessing.Queue | Communication between processes |
heapq | Priority-based processing |
YT:- DecodeIT
Applications of Queue in Python
Queues are useful in many real-world and programming situations, including:
- Printer job management
- Operating system task scheduling
- Data buffering
- Background task processing
- Multiprocessing communication
- Customer service systems
- Network request handling
Conclusion
A queue is an important data structure that follows the FIFO (First In, First Out) principle. Python provides different ways to implement queues depending on the requirements of an application.
For simple learning examples, a list can be used. For efficient general-purpose queue operations, collections.deque is a useful option. For thread-safe applications, queue.Queue is suitable, while multiprocessing.Queue can be used for communication between processes.
Understanding queues is important for learning data structures and building programs involving scheduling, task management, buffering, and ordered processing.
Frequently Asked Questions
What is a queue in Python?
A queue is a linear data structure that follows the FIFO principle, where the first element added is the first element removed.
What is FIFO in a queue?
FIFO stands for First In, First Out. It means the element that enters the queue first is processed first.
Which Python module is used for queues?
Python provides the built-in queue module, which includes the Queue class for creating thread-safe queues.
Why is deque better than a list for a queue?
deque provides efficient operations at both ends. In particular, removing an item from the left using popleft() is efficient compared with list.pop(0).
How do you add an element to a Python queue?
With queue.Queue, use the put() method. With deque, you can use append().
Keywords
Queue in Python, queue in python, implement queue in python, Python queue example, queue data structure in Python, FIFO in Python, Python queue module, Python Queue class, deque Python, collections deque Python, priority queue in Python, multiprocessing Queue Python, types of queue in Python, circular queue in Python, stack in Python, linked list in Python, set in Python