Queue in Python
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 first-come, first-served rule is followed.

Operations associated with queue are:
- Enqueue: Adds an item to the queue. If queue is full, it is said to be an Overflow condition – Time Complexity : O(1)
- Dequeue: Removes an item from the queue. If the queue is empty, it is said to be an Underflow condition – Time Complexity : O(1)
- Front: Get front item from queue – Time Complexity : O(1)
- Rear: Get last item from queue – Time Complexity : O(1)
Implement a Queue
There are various ways to implement a queue in Python by following ways:
1. Implementation using list
Lists can be used as queues, but removing elements from front requires shifting all other elements, making it O(n).
Example: Simulate a queue with a Python list.
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue:", queue)
print("Elements dequeued from queue:")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("Queue after removing elements:", queue)
Output
Initial queue: ['a', 'b', 'c'] Elements dequeued from queue: a b c Queue after removing elements: []
Explanation: We added elements using append() and removed from the front using pop(0). After removing all elements, queue is empty.
2. Implementation using collections.deque
deque (double-ended queue) is preferred over a list for queues because both append() and popleft() run in O(1) time.
Example: Queue using deque.
from collections import deque
q = deque()
q.append('a')
q.append('b')
q.append('c')
print("Initial queue:", q)
print("Elements dequeued from the queue:")
print(q.popleft())
print(q.popleft())
print(q.popleft())
print("Queue after removing elements:", q)
Output
Initial queue: deque(['a', 'b', 'c']) Elements dequeued from the queue: a b c Queue after removing elements: deque([])
Explanation: popleft() efficiently removes the first element without shifting, making deque ideal for queues.
3. Implementation using queue.Queue
Python’s queue module provides a thread-safe FIFO queue. You can specify a maxsize. Key Methods are:
- put(item) / put_nowait(item) – Add an element.
- get() / get_nowait() – Remove an element.
- empty() – Check if the queue is empty.
- full() – Check if the queue is full.
- qsize() – Get current size of the queue.
Example: Queue using queue.Queue.
from queue import Queue
q = Queue(maxsize=3)
print("Initial size:", q.qsize())
q.put('a')
q.put('b')
q.put('c')
print("Is full:", q.full())
print("Elements dequeued from the queue:")
print(q.get())
print(q.get())
print(q.get())
print("Is empty:", q.empty())
q.put(1)
print("Is empty:", q.empty())
print("Is full:", q.full())
Output
Initial size: 0 Is full: True Elements dequeued from the queue: a b c Is empty: True Is empty: False Is full: False
Explanation: queue.Queue class handles thread-safe operations. You can check fullness or emptiness before adding or removing elements.
To practice problems related to Queue, refer to this article Queue Data Structure