How to Get the length of a Queue in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. Get the length of a Queue in Python
  2. Convert a List to a Queue in Python
  3. Create a Fixed-size Queue in Python
  4. Clear all items from a Queue in Python

# Get the length of a Queue in Python

To get the length of a queue in Python:

  1. Use the len() function to get the length of a deque object.
  2. Use the qsize() method to get the length of a queue object.
main.py
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

get length of queue

The code for this article is available on GitHub

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.

main.py
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.

main.py
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.

main.py
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 code for this article is available on GitHub

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.

# Convert a List to a Queue in Python

You can use the collections.deque class to convert a list to a queue in Python.

main.py
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

convert list to queue

The code for this article is available on GitHub

The collections.deque() class takes an iterable and initializes a deque object (a double-ended queue).

The class has atomic append(), implements the popleft() method and supports indexing and membership testing.

# Use a for loop when the queue is created via the queue module

If you need to use the queue module to create your queue, use a for loop to convert the list to a queue.

main.py
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])

use for loop when queue is crated via queue module

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.

You should only use the 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.

main.py
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([])
The code for this article is available on GitHub

# Create a Fixed-size Queue in Python

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.

main.py
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)

create fixed size queue

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.

main.py
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.

If the 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.

main.py
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.

main.py
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)
The code for this article is available on GitHub

# Clear all items from a Queue in Python

If you need to clear all items from a queue in Python:

  1. Use the queue attribute on the queue to get a deque object.
  2. Call the clear() method on the deque object.
  3. The clear method will remove all elements from the queue.
main.py
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([])

clear all items from queue

The queue attribute points to a deque object, and deque objects implement a clear method.

The 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.

main.py
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.

main.py
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([])
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev