- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Queue.LIFOQueue vs Collections.Deque in Python
In this article we will learn about Queue.LIFOQueue vs Collections.Deque in Python programming language. When we need to manage our data using the last in first out method then we can use these data structures. But to choose one of them we need to know about their functionalities and characteristics. Before that let’s get to know about Queue.LIFOQueue and Collections.Deque.
LIFOQueue
This class is part of the queue module. It works as stack data structure and it is considered as thread safe means we can talk between the different threads at the same time. Here are some specifications of this class.
Stack based − LIFOQueue behaves like a stack data structure which says the item which is inserted at last will come at first after retrieval.
Thread safe − Using this class we can apply the synchronization mechanism and use it like multi-threaded applications.
Blocking I/o operation − In Queue.LIFOQueue we have the option of blocking the thread using get() and put(). It makes the thread wait until the criteria is fulfilled like item is available or the queue is empty.
Size − Queue.LIFOQueue has a size parameter which limits the size of the queue and limits the number of items which have to be inserted.
Let’s see an Example of the Queue.LIFOQueue.
Example
class LifoQueueClass: def __init__(self): self.ele = [] def is_empty(self): return len(self.ele) == 0 def push(self, item): self.ele.append(item) def pop(self): if self.is_empty(): return None return self.ele.pop() def size(self): return len(self.ele) def display(self): print("LIFO Queue: ", self.ele) queueDs = LifoQueueClass() queueDs.push("Banana") queueDs.push("Apple") queueDs.push("Guava") queueDs.push("Litchi") print("Updated Queue After inserting items: ") queueDs.display() removed_item = queueDs.pop() print("\nPopped item:", removed_item) print("Updated Queue After pop: ") queueDs.display() print("\nQueue size:", queueDs.size()) if queueDs.is_empty(): print("Queue is empty\n") else: print("Queue is not empty\n")
Output
Updated Queue After inserting items: LIFO Queue: ['Banana', 'Apple', 'Guava', 'Litchi'] Popped item: Litchi Updated Queue After pop: LIFO Queue: ['Banana', 'Apple', 'Guava'] Queue size:3 Queue is not empty
Explanation
Here in the program we create a queue class named LifoQueueClass. Inside the class we have different methods like push to insert items into the queue, pop for removing items from the queue, size to check, is_empty for checking if the queue is empty means there is no item left inside the queue. We make an object of the queue class and using that object we will call the push function and pass the items which we want to insert. Using the display we can display items which are there inside the queue.
Collections.deque
This is a class which is part of the collection module in python. It works as a dequeue (double ended queue) and it has many functionalities, here are some specifications −
Double-Ended Queue based − Collections.deque behaves like a double-ended queue data structure which supports operations of insertion and deletion from both the ends. It has many methods like append(), appendLeft() to insert elements into the dequeue and pop(), popLeft() to remove items from the dequeue.
Size − Unlike LIFOQueue dequeue doesn’t have maximum size parameter, it can dynamically increase and decrease according to the length of the items.
Efficient − Dequeue provides efficient operation for inserting and retrieving the element from both the end in constant time complexity.
Let’s see an example of the Collection.deque.
Example
from collections import deque Dqueue = deque() Dqueue.append("Banana") Dqueue.append("Apple") Dqueue.append("Guava") Dqueue.append("Litchi") print("Queue:", Dqueue) ele = Dqueue.popleft() print("Removed element:", ele) print("\nUpdated Queue:", Dqueue) if not Dqueue: print("Queue is empty.") else: print("Queue is not empty.") Dqueue.append("Mango") Dqueue.append("Papaya") print("\nUpdated Queue:", Dqueue) print("\nQueue size:", len(Dqueue), "\n") while Dqueue: ele = Dqueue.popleft() print("Dequeued element:", ele) if not Dqueue: print("Queue is empty.") else: print("Queue is not empty.")
Output
Queue: deque(['Banana', 'Apple', 'Guava', 'Litchi']) Removed element: Banana Updated Queue: deque(['Apple', 'Guava', 'Litchi']) Queue is not empty. Updated Queue: deque(['Apple', 'Guava', 'Litchi', 'Mango', 'Papaya']) Queue size: 5 Dequeued element: Apple Dequeued element: Guava Dequeued element: Litchi Dequeued element: Mango Dequeued element: Papaya Queue is empty.
Explanation
Here in the program, we create an empty queue using deque. For adding elements, we use the append() method and pass the element which has to be inserted into the queue. Using the popLeft() method we removed the item from the queue. Then we check if the queue is empty or not and append two items more into the queue using queue.append("Mango"). To remove all the items from the queue we use while loop and print the items which are being removed.
So, we saw the difference between Queue.LIFOQueue and Collections.Deque. We got to know about some key features about both of the data structures. We saw different methods to insert and retrieve the element from the LIFOQueue and Deque. Using programs of both datastructures we got to know the working of both the data structures. So, deque is considered as an efficient data structure as it allows us to manipulate the data from both the ends in constant time. Deque also allows us to retrieve the data using index but it is having less efficiency than retrieving from the end.