Last updated: Apr 8, 2024
Reading timeยท5 min
To get the length of a queue in Python:
len()
function to get the length of a deque object.qsize()
method to get the length of a queue object.from collections import deque import queue deq = deque(['a', 'b', 'c']) # โ Get the length of a deque object print(len(deq)) # ๐๏ธ 3 q = queue.Queue() for item in range(15): q.put(item) # โ Get the length of a queue object print('size of queue: ', q.qsize()) # ๐๏ธ 15
We used the len()
function to get the length of a deque object.
The len() function returns the length (the number of items) of an object.
from collections import deque deq = deque(['a', 'b', 'c', 'd']) # ๐๏ธ Get the length of a deque object print(len(deq)) # ๐๏ธ 4
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).
The len()
function is always guaranteed to return the correct length of the
deque
object, regardless if you add or remove items.
from collections import deque deq = deque(['a', 'b']) deq.append('c') deq.append('d') print(len(deq)) # ๐๏ธ 4 deq.remove('d') print(len(deq)) # ๐๏ธ 3
If you use the queue
module, use the qsize()
method to get the length of the
queue.
import queue q = queue.Queue() for item in range(15): q.put(item) # ๐๏ธ Get the length of a queue object print('size of queue: ', q.qsize()) # ๐๏ธ 15 # ๐๏ธ Check if queue is empty print(q.empty()) # ๐๏ธ False
The Queue.qsize() method returns the approximate size of the queue.
You can use the Queue.empty() method to check if the queue is empty.
You can use the collections.deque
class to convert a list to a queue in
Python.
from collections import deque my_list = [1, 2, 3] # โ Convert list to queue deq = deque(my_list) print(deq) # ๐๏ธ deque([1, 2, 3]) deq.append(4) print(deq) # ๐๏ธ deque([1, 2, 3, 4]) print(deq.pop()) # ๐๏ธ 4 print(deq.popleft()) # ๐๏ธ 1
The
collections.deque()
class takes an iterable and initializes a deque
object (a double-ended queue).
append()
, implements the popleft()
method and supports indexing and membership testing.for
loop when the queue is created via the queue
moduleIf you need to use the queue
module to create your queue, use a
for loop to convert the list to a queue.
import queue my_list = [1, 2, 3] q = queue.Queue() for item in my_list: q.put(item) print(q.queue) # ๐๏ธ deque([1, 2, 3])
The Queue.put() method puts an item in the queue.
The queue
attribute on the queue points to a deque
object which is much more
full-featured.
queue
module if you are running your code in a multi-threaded environment.Here are some of the most commonly used methods the deque
class implements.
from collections import deque my_list = [1, 2, 3] deq = deque(my_list) # ๐๏ธ Add a value to the right side of the deque deq.append(4) # ๐๏ธ Add a value to the left side of the deque deq.appendleft(0) # ๐๏ธ Extend the right side of the deque by appending the items from the iterable deq.extend([5, 6]) # ๐๏ธ Remove and return an element from the right side of the deque print(deq.pop()) # ๐๏ธ 6 # ๐๏ธ Remove and return an element from the left side of the deque print(deq.popleft()) # ๐๏ธ 0 print(deq) # ๐๏ธ deque([1, 2, 3, 4, 5]) deq.clear() print(deq) # ๐๏ธ deque([])
You can use the deque class to create a fixed-size queue.
The class takes a maxlen
argument that determines the maximum length of the
deque.
from collections import deque deq = deque(maxlen=3) deq.extend([1, 2, 3]) print(deq) # ๐๏ธ deque([1, 2, 3], maxlen=3) deq.append(4) print(deq) # ๐๏ธ deque([2, 3, 4], maxlen=3)
We used the
collections.deque
class to initialize a deque
object.
You can also pass an iterable to the class to initialize the deque
object with
items.
from collections import deque # ๐๏ธ Initialize deque with items deq = deque([1, 2, 3], maxlen=3) print(deq) # ๐๏ธ deque([1, 2, 3], maxlen=3) deq.append(4) print(deq) # ๐๏ธ deque([2, 3, 4], maxlen=3)
The deque
class takes a maxlen
argument. If the argument is not provided or
is None
, the deque
object can grow to an arbitrary length.
maxlen
argument is provided and the deque
is full, when new items are added, the same number of items are discarded from the opposite end.If we
append
a new item to the deque
, an item from the start of the deque
is discarded.
from collections import deque deq = deque([1, 2, 3], maxlen=3) print(deq) # ๐๏ธ deque([1, 2, 3], maxlen=3) deq.append(4) print(deq) # ๐๏ธ deque([2, 3, 4], maxlen=3)
Conversely, if we use the
appendleft()
method to add an item to the left side of the deque
, an item from the end of
the deque
is discarded.
from collections import deque deq = deque([1, 2, 3], maxlen=3) print(deq) # ๐๏ธ deque([1, 2, 3], maxlen=3) deq.appendleft(0) print(deq) # ๐๏ธ deque([0, 1, 2], maxlen=3)
If you need to clear all items from a queue in Python:
queue
attribute on the queue to get a deque
object.clear()
method on the deque
object.clear
method will remove all elements from the queue.import queue q = queue.Queue() for item in range(10): q.put(item) print(q.queue) # ๐๏ธ deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) q.queue.clear() print(q.queue) # ๐๏ธ deque([])
The queue
attribute points to a deque
object, and deque
objects implement
a
clear
method.
clear()
method removes all elements from the deque and leaves it with a length of 0
.If you need to make the operation thread-safe, use a mutex
lock.
import queue q = queue.Queue() for item in range(10): q.put(item) print(q.queue) # ๐๏ธ deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # ๐๏ธ use mutex lock with q.mutex: q.queue.clear() print(q.queue) # ๐๏ธ deque([])
You can use a mutex
lock if your code runs in a multithreaded environment.
An alternative is to simply create a new queue and delete the old one.
import queue q = queue.Queue() for item in range(10): q.put(item) del q # ๐๏ธ delete old queue new_q = queue.Queue() print(new_q.queue) # ๐๏ธ deque([])
You can learn more about the related topics by checking out the following tutorials: