Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Queue.LifoQueue vs Collections.Deque

Last Updated on August 30, 2022 by Gokul Kannan

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.

queue.LifoQueue

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())

collections.deque

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)

Sr. No.LifoQueueDeque
1It implements stack data
structure.
It implements a doubly ended
queue data structure.
2It is present in the queue
modules in Python.
It is present in the collections
module in Python.
3It allows various threads to
communicate using queued
data or messages.
It is simply intended to be data
structure.
4It has fewer features
(operations and methods).
It has more features (operation
and methods).
5It follows LIFO
(Last in First out).
It follows FIFO
(First in First out).
6The operations of LifoQueue
are slow (long execution time).
The operations of deque are
fast (very low execution time).
7It is not commonly used, usually
for thread communication
operations.
It is mostly used for data
structure operations.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *