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.


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.


class LifoQueueClass:
   def __init__(self):
      self.ele = []

   def is_empty(self):
      return len(self.ele) == 0

   def push(self, 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()


print("Updated Queue After inserting items: ")

removed_item = queueDs.pop()
print("\nPopped item:", removed_item)

print("Updated Queue After pop: ")

print("\nQueue size:", queueDs.size())

if queueDs.is_empty():
   print("Queue is empty\n")
   print("Queue is not empty\n")


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


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.


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.


from collections import deque

Dqueue = deque()


print("Queue:", Dqueue)

ele = Dqueue.popleft()
print("Removed element:", ele)

print("\nUpdated Queue:", Dqueue)

if not Dqueue:
   print("Queue is empty.")
   print("Queue is not empty.")


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.")
   print("Queue is not empty.")


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.


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.

Updated on: 03-Oct-2023


Kickstart Your Career

Get certified by completing the course

Get Started