Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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, we can use these data structures. But to choose one of them we need to know about their functionalities and characteristics.
Queue.LIFOQueue
This class is part of the queue module. It works as a stack data structure and is thread-safe, meaning we can communicate between different threads simultaneously. Here are some key specifications:
Stack-based LIFOQueue behaves like a stack data structure where the item inserted last will be retrieved first.
Thread-safe This class provides synchronization mechanisms for multi-threaded applications.
Blocking operations LIFOQueue supports blocking operations with
get()andput()methods that can make threads wait until conditions are met.Size limit LIFOQueue has an optional size parameter to limit the maximum number of items.
Example
from queue import LifoQueue
# Create a LIFO queue with maximum size of 5
lifo_queue = LifoQueue(maxsize=5)
# Add items to the queue
items = ["Banana", "Apple", "Guava", "Litchi"]
for item in items:
lifo_queue.put(item)
print("Queue size:", lifo_queue.qsize())
# Remove items (LIFO order)
print("\nRemoving items from LIFO queue:")
while not lifo_queue.empty():
item = lifo_queue.get()
print("Removed:", item)
print("Queue is empty:", lifo_queue.empty())
Queue size: 4 Removing items from LIFO queue: Removed: Litchi Removed: Guava Removed: Apple Removed: Banana Queue is empty: True
Collections.deque
This class is part of the collections module. It works as a double-ended queue (deque) with efficient operations at both ends. Here are its key specifications:
Double-ended Supports insertion and deletion from both ends using methods like
append(),appendleft(),pop(), andpopleft().Dynamic size Unlike LIFOQueue, deque doesn't have a maximum size limit and can grow dynamically.
Efficient operations Provides O(1) time complexity for operations at both ends.
Not thread-safe Requires external synchronization for multi-threaded environments.
Example
from collections import deque
# Create a deque
dq = deque()
# Add items to the right end
items = ["Banana", "Apple", "Guava", "Litchi"]
for item in items:
dq.append(item)
print("Deque:", dq)
# Remove from left (FIFO behavior)
removed = dq.popleft()
print("Removed from left:", removed)
# Remove from right (LIFO behavior)
removed = dq.pop()
print("Removed from right:", removed)
print("Updated deque:", dq)
print("Deque length:", len(dq))
Deque: deque(['Banana', 'Apple', 'Guava', 'Litchi']) Removed from left: Banana Removed from right: Litchi Updated deque: deque(['Apple', 'Guava']) Deque length: 2
Comparison
| Feature | Queue.LIFOQueue | Collections.deque |
|---|---|---|
| Thread Safety | Yes (built-in) | No (manual sync needed) |
| Operations | Stack (LIFO only) | Double-ended (both ends) |
| Size Limit | Optional maxsize | Dynamic (no limit) |
| Blocking | Yes | No |
| Performance | Good with thread safety | Excellent (O(1) both ends) |
When to Use Which?
Use Queue.LIFOQueue when:
- You need thread-safe operations in multi-threaded applications
- You want blocking behavior for producer-consumer scenarios
- You need a simple stack with size limitations
Use Collections.deque when:
- You need efficient operations at both ends
- Working in single-threaded environments
- You need maximum performance for stack/queue operations
- You want flexibility with various insertion/deletion patterns
Conclusion
Choose Queue.LIFOQueue for thread-safe stack operations with blocking capabilities. Use collections.deque for high-performance double-ended operations in single-threaded scenarios. Deque offers more flexibility and better performance, while LIFOQueue provides thread safety and blocking behavior.
