How to Use Queues in Python
Queues are a fundamental data structure used for managing ordered collections of items, often employed in scenarios requiring FIFO (First-In-First-Out) behavior.
This guide explores how to get the length of a queue, convert lists to queues, create fixed-size queues, and clear queues in Python, using the collections
and queue
modules.
Getting the Length of a Queue
To determine the number of items currently in a queue, use different methods depending on the type of queue you are using.
1. Using len()
with deque
Objects
If you use collections.deque
, you can use the len()
function to get the length:
from collections import deque
deq = deque(['a', 'b', 'c'])
print(len(deq)) # Output: 3
- The
len()
method will return the number of elements that are stored in thedeque
object at any given time.
2. Using qsize()
with queue.Queue
Objects
If you are using the queue.Queue
class, use the qsize()
method instead:
import queue
q = queue.Queue()
for item in range(15):
q.put(item)
print('size of queue: ', q.qsize()) # Output: 15
print(q.empty()) # Output: False
- The
qsize()
method gives you the current (approximate) number of items in the queue. - Use the
empty()
method to check if the queue is empty.
Converting Lists to Queues
Often you need to create a queue from an existing list.
1. Using collections.deque
The collections.deque
class is a double-ended queue. You can convert a list to a deque
object directly:
from collections import deque
my_list = [1, 2, 3]
deq = deque(my_list)
print(deq) # Output: deque([1, 2, 3])
deq.append(4)
print(deq) # Output: deque([1, 2, 3, 4])
print(deq.pop()) # Output: 4
print(deq.popleft()) # Output: 1
- The
deque()
constructor takes an iterable and initializes the object with the items from it. - The class also has atomic
append()
,pop()
andpopleft()
methods for safe manipulation of the queue data.
2. Using a for
Loop with queue.Queue
If you are using the queue module, use a for
loop with put()
to add all list items to the queue:
import queue
my_list = [1, 2, 3]
q = queue.Queue()
for item in my_list:
q.put(item)
print(q.queue) # Output: deque([1, 2, 3])
- The
Queue.put()
method adds an item to the queue. - The
queue
attribute of thequeue.Queue
class points to adeque
object. - Use the
queue
module for multi-threaded environments only.
Here are some of the common methods the deque
object implements:
from collections import deque
my_list = [1, 2, 3]
deq = deque(my_list)
deq.append(4) # Append to the right
deq.appendleft(0) # Append to the left
deq.extend([5, 6]) # Extend from right with list elements
print(deq.pop()) # Remove and return element from right
print(deq.popleft()) # Remove and return element from left
print(deq) # Output: deque([1, 2, 3, 4, 5])
deq.clear()
print(deq) # Output: deque([])
Creating a Fixed-Size Queue with deque
The deque
class allows you to create a fixed-size queue using the maxlen
argument:
from collections import deque
deq = deque(maxlen=3)
deq.extend([1, 2, 3])
print(deq) # Output: deque([1, 2, 3], maxlen=3)
deq.append(4)
print(deq) # Output: deque([2, 3, 4], maxlen=3)
deq.appendleft(0)
print(deq) # Output: deque([0, 2, 3], maxlen=3)
- If
maxlen
is provided the object will maintain that size, removing elements from the opposite end when adding new items. - If no
maxlen
argument is specified the queue can grow indefinitely.
Clearing all Items from a Queue
To remove all items from a queue, there are several different approaches.
1. Clearing a queue.Queue
Object
To clear a queue created using the queue
module, use the clear()
method on the underlying deque
object:
import queue
q = queue.Queue()
for item in range(10):
q.put(item)
print(q.queue) # Output: deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
q.queue.clear()
print(q.queue) # Output: deque([])
- The
q.queue
attribute accesses the underlyingdeque
object which has aclear()
method.
2. Thread Safe Clearing
If you are using queue.Queue
in a multi-threaded environment, make sure that your clear operations are thread-safe.
import queue
q = queue.Queue()
for item in range(10):
q.put(item)
with q.mutex:
q.queue.clear()
print(q.queue) # Output: deque([])
- Use the
with q.mutex:
block to use a mutex lock for thread safety.
3. Creating a New Queue
Alternatively you can simply create a new queue to effectively discard the old data:
import queue
q = queue.Queue()
for item in range(10):
q.put(item)
del q
new_q = queue.Queue()
print(new_q.queue) # Output: deque([])
- The old queue
q
is deleted, and a new queuenew_q
is created.