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
Dumping queue into list or array in Python
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. While queues only allow access to the front element, sometimes we need to convert the entire queue into a list for easier manipulation. Python provides multiple approaches to dump queue contents into a list.
Creating a Queue in Python
Python offers two main queue implementations: collections.deque and queue.Queue. Let's first see how a basic queue works ?
from collections import deque
# Create and populate a queue
queue = deque()
queue.append(1)
queue.append(2)
queue.append(3)
queue.append(4)
print("Queue contents:", queue)
# Remove first element (FIFO)
first_element = queue.popleft()
print("Removed element:", first_element)
print("Queue after removal:", queue)
Queue contents: deque([1, 2, 3, 4]) Removed element: 1 Queue after removal: deque([2, 3, 4])
Method 1: Using collections.deque
The deque (doubleended queue) can be easily converted to a list using the list() constructor ?
from collections import deque
# Create and populate the queue
queue = deque([10, 20, 30, 40, 50])
print("Original queue:", queue)
print("Queue type:", type(queue))
# Convert queue to list
queue_list = list(queue)
print("Converted to list:", queue_list)
print("List type:", type(queue_list))
# Original queue remains unchanged
print("Original queue after conversion:", queue)
Original queue: deque([10, 20, 30, 40, 50]) Queue type: <class 'collections.deque'> Converted to list: [10, 20, 30, 40, 50] List type: <class 'list'> Original queue after conversion: deque([10, 20, 30, 40, 50])
Method 2: Using queue.Queue
The queue.Queue class is threadsafe but requires accessing the internal .queue attribute for conversion ?
from queue import Queue
# Create and populate the queue
q = Queue()
q.put('A')
q.put('B')
q.put('C')
q.put('D')
print("Queue contents:", q.queue)
print("Queue type:", type(q))
# Convert to list using the internal queue attribute
queue_list = list(q.queue)
print("Converted to list:", queue_list)
print("List type:", type(queue_list))
Queue contents: deque(['A', 'B', 'C', 'D']) Queue type: <class 'queue.Queue'> Converted to list: ['A', 'B', 'C', 'D'] List type: <class 'list'>
Method 3: Draining Queue Contents
For threadsafe operations, you can drain the queue contents while building a list ?
from queue import Queue
# Create and populate the queue
q = Queue()
for item in [100, 200, 300, 400]:
q.put(item)
print("Queue size before draining:", q.qsize())
# Drain queue into list
queue_list = []
while not q.empty():
queue_list.append(q.get())
print("Drained list:", queue_list)
print("Queue size after draining:", q.qsize())
Queue size before draining: 4 Drained list: [100, 200, 300, 400] Queue size after draining: 0
Comparison
| Method | Preserves Queue | Thread-Safe | Use Case |
|---|---|---|---|
list(deque) |
Yes | No | Single-threaded, simple conversion |
list(queue.queue) |
Yes | No | Access internal structure |
Draining with get()
|
No | Yes | Thread-safe processing |
Conclusion
Use list(deque) for simple conversions in singlethreaded applications. For threadsafe operations, drain the queue using get() in a loop. Both approaches have O(n) time and space complexity.
