Both LIFOQueue and Deque are present in the built-in modules Queue and Collections in Python, both of them are widely used data structures, but they are used for different purposes. In this article ,we will discuss the difference between both on the basis of usability, execution time, working, implementation, etc. in Python.
LifoQueue is present in the queue module in Python which has the technical same working as a Stack data structures. As the name suggests in LifoQueue, the element inserted at last will be removed first. It is used for communication between the different threads in the very same process.
Implementation of queue.LifoQueue
import queue LIFOq = queue.LifoQueue(maxsize=3) print(LIFOq.qsize()) LIFOq.put(12) LIFOq.put(24) LIFOq.put(36) print("Full: ", LIFOq.full()) print("Size: ", LIFOq.qsize()) print(LIFOq.get()) print(LIFOq.get()) print(LIFOq.get()) print("Empty: ", LIFOq.empty())
Deque ( Doubly Ended Queue ) is present in the collections module in Python. In this data structure, insertion and deletion can be done on the both rear and front end. Operations of deque are quite faster than list in Python.
Implementation of collections.deque
import collections Deque = collections.deque([12, 24, 36]) Deque.append(0) print("The deque after appending at right is:", Deque) Deque.appendleft(100) print("The deque after appending at left is: ", Deque) Deque.pop() print("The deque after deleting from right is:", Deque); Deque.popleft() print("Queue:", Deque)
|1||It implements stack data |
|It implements a doubly ended |
queue data structure.
|2||It is present in the queue |
modules in Python.
|It is present in the collections |
module in Python.
|3||It allows various threads to |
communicate using queued
data or messages.
|It is simply intended to be data |
|4||It has fewer features |
(operations and methods).
|It has more features (operation |
|5||It follows LIFO |
(Last in First out).
|It follows FIFO |
(First in First out).
|6||The operations of LifoQueue |
are slow (long execution time).
|The operations of deque are |
fast (very low execution time).
|7||It is not commonly used, usually |
for thread communication
|It is mostly used for data |
This article tried to discuss Queue.LifoQueue vs Collections.Deque. Hope this blog helps you understand the concept. To practice problems feel free to check MYCODE | Competitive Programming at PrepBytes.