Creating Queue Using Collections.Deque In Python
Last Updated :
24 Apr, 2024
In Python, Queue and Deque are the data structures used for managing collections of elements in a first-in, first-out (FIFO) manner. In this article, we will learn to implement queues using collections.deque in Python.
Create Queue Using Collections.deque In Python
Below, is the Implementation of Creating Queue Using Collections.Deque in Python:
Step 1: Importing collections.deque
In the below step, we import the deque class from the collections module using the statement from the collections import deque. This allows us to access the functionality of the deque data structure.
Python3
from collections import deque
Step 2: Defining the Queue Class
In the below step, we define the Queue class with methods to manipulate a queue data structure. The __init__ method initializes an empty deque to store queue elements. The enqueue, dequeue, peek, is_empty, and size methods handle adding, removing, peeking at the front, checking emptiness, and getting the size of the queue, respectively, using the deque's operations.
Python3
class Queue:
def __init__(self):
self.queue = deque()
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if not self.is_empty():
return self.queue.popleft()
else:
raise IndexError("dequeue from an empty queue")
def peek(self):
if not self.is_empty():
return self.queue[0]
else:
raise IndexError("peek from an empty queue")
def is_empty(self):
return len(self.queue) == 0
def size(self):
return len(self.queue)
Step 3: Using the Queue
In the below step, we perform how to use the Queue class we defined. We create a queue instance named queue, enqueue elements 10, 20, and 30 into the queue, dequeue an element from the queue, peek at the front element, check if the queue is empty, and get its size.
Python3
# Create a queue instance
queue = Queue()
# Enqueue elements into the queue
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
# Dequeue an element from the queue
dequeued_item = queue.dequeue()
print("Dequeued item:", dequeued_item)
# Peek at the front of the queue
front_item = queue.peek()
print("Front item:", front_item)
# Check if the queue is empty
print("Is queue empty?", queue.is_empty())
# Get the size of the queue
print("Queue size:", queue.size())
Complete Code
Below, is the complete code for creating a queue using collections.deque in Python.
main.py
Python3
from collections import deque
class Queue:
def __init__(self):
self.queue = deque()
def enqueue(self, item):
self.queue.append(item)
def dequeue(self):
if not self.is_empty():
return self.queue.popleft()
else:
raise IndexError("dequeue from an empty queue")
def peek(self):
if not self.is_empty():
return self.queue[0]
else:
raise IndexError("peek from an empty queue")
def is_empty(self):
return len(self.queue) == 0
def size(self):
return len(self.queue)
# Using the Queue
queue = Queue()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
dequeued_item = queue.dequeue()
print("Dequeued item:", dequeued_item)
front_item = queue.peek()
print("Front item:", front_item)
print("Is queue empty?", queue.is_empty())
print("Queue size:", queue.size())
OutputDequeued item: 10
Front item: 20
Is queue empty? False
Queue size: 2
Similar Reads
Enqueue in Queues in Python A queue is a basic data structure that follows the First-In-First-Out (FIFO) principle. At the back of the queue, elements are added (enqueued), and at the front, they are removed (dequeued). In this article, we will see the methods of Enqueuing (adding elements) in Python. Enqueue: The act of addin
2 min read
Creating Sets of Tuples in Python Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read
Creating a List of Sets in Python Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python.Using List ComprehensionList comprehen
2 min read
Creating Dictionary of Sets in Python The task of creating a dictionary of sets in Python involves storing multiple unique values under specific keys, ensuring efficient membership checks and eliminating duplicates. For example, given student data where "Roll-no" maps to {1, 2, 3, 4, 5} and "Aadhaar No" maps to {11, 22, 33, 44, 55}, the
3 min read
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read