Get size of priority queue in python [SOLVED]


Python

Author: Bashir Alam
Reviewer: Deepak Prasad

Introduction to Queue in Python

A queue is a basic data structure in programming. To manage, store, and organize data, data structures, and queues are mostly used. They facilitate program comprehension and writing and are frequently quicker.

A queue conceptually shows data elements as an ordered list. The list is cleared of items in the order they were added. Queues are a common idea in daily life.    A queue, for instance, is a group of individuals standing in line at a bank or coffee shop. The front of the line is occupied by the first client. They will receive service after them. A new consumer joins the back of the line when they arrive.

get-size-of-priority-queue-structure-of-queue

As shown above, queues are frequently used to process items using a first in first out (FIFO) strategy. In computer terms, push and pop operations are used to service queues. When it's time for them to be processed, items are pushed onto the queue and removed from it.

 

What is the priority Queue in Python?

As we discussed earlier that queues are frequently used to process items using a first in first out (FIFO) strategy. However, it is often necessary to account for the priority of each item when determining processing orders. A queue that retrieves and removes items based on their priority as well as their arrival time is called a priority queue. Prioritization can be complicated, but fortunately, Python priority queues can be easily and efficiently implemented using a built-in module.

A priority queue maintains numerous sub-queues for the various priority levels while still adhering to fundamental queueing concepts. The entries are pre-sorted according to their priority. Even if lower-priority items arrived earlier, the highest-priority entries are always handled first. In reality, a priority queue's core mechanism rarely creates numerous lists. Instead, the new item is inserted according to the priority. As a result, the things at the front of the line are always served first. However, new products do not necessarily join the line at the back. It is even feasible to move a very high-priority item to the front of the line.

 

How to use the Priority Queue class in Python?

You can create your own priority queues in Python by using Python lists. However, it is better to use the built-in PriorityQueue class. This class handles all fundamental operations, including put and get, very effectively. Python automatically updates the underlying structure of the queues and adds and deletes entries based on their priority.

Let us import the PriorityQueue in Python and initialize a queue.

# importing priority queue
from queue import PriorityQueue

# initializing priority queue
Queue = PriorityQueue()

As you can see, we have imported the PriorityQueue and initialized it. The PriorityQueue method takes a parameter value as the size of the queue. For example, passing 50 to the PriorityQueue method will initialize a queue with the size of 50.

Now let us create an empty queue and add items in their priority level and pop those elements.

# importing priority queue
from queue import PriorityQueue

# initializing priority queue
Queue = PriorityQueue()

# adding elements in priority level
Queue.put((2, "Bashir"))
Queue.put((1, "alam"))
Queue.put((3, "Erlam"))

As shown above, we have created a queue and added elements based on their priority level. Now let us pop the first element from the queue. It will be the element with the highest priority level as shown below:

# getting elements 
element = Queue.get()

# printing element
print(element)

output:

(1, 'alam')

Although 'alam' element was added in the second position because of its high priority level, it popped out first.

 

Examples to get size of priority queue in Python

As we discussed that priority Queue is a special type of queue that arranges elements based on their priority level. In this section, we will discuss how we can get size of priority queue in Python by solving various examples.

  • Get size of the empty priority queue
  • Get the size of priority queue by adding elements
  • Get the size of priority queue by deleting elements

 

Example-1: Get the size of the empty Priority Queue in Python

PriorityQueue class has many useful methods and one of them is qsize() method which is used to return the size of the queue.

For example, let us create a queue and use the qsize() method to get the size of the queue.

# importing priority queue
from queue import PriorityQueue

# initializing priority queue
Queue = PriorityQueue()

# printing get size of priority queue in python
print('the size of queue is:', Queue.qsize())

Output:

the size of queue is: 0

As shown above, the size of the queue is 0 because there are no elements added there.

 

Example 2: Get size of priority queue in Python by adding elements

Now we will add elements to the queue and get the size of the queue. Let us create an empty queue and then add elements before getting the size.

# importing priority queue
from queue import PriorityQueue

# initializing priority queue
Queue = PriorityQueue()

# adding elements in priority level
Queue.put(("Bashir"))
Queue.put(("alam"))

# get size of priority queue in python
print('the size of queue is:', Queue.qsize())

Output:

the size of queue is: 2

As you can see, the size of the queue is 2 because we have added 2 elements.

 

Example 3: Get size of priority queue in Python by removing elements

Now let us get the size of the priority queue by removing elements. First, we will create a queue and add elements to get the size of the queue, and then we will remove the elements from it and see how it affects the size of the queue.

# initializing priority queue
Queue = PriorityQueue()

# adding elements in priority level
Queue.put((4, "Bashir"))
Queue.put((2, "alam"))
Queue.put((3, "erlan"))
Queue.put((1, "al"))


# get size of priority queue in python
print('the size of queue is:', Queue.qsize())

Output:

the size of queue is: 4

Now let us remove some elements and get the size of the priority queue again. The get method returns the element from the queue.

# getting elements from queue
Queue.get()

# get size of priority queue in python
print('the size of queue is:', Queue.qsize())

Output:

the size of queue is: 3

As you can see, the size of the queue is 3 now because we have removed one element from it.

 

Summary

Queue in Python Programming is one of the linear data structures used to store data in memory. The queue is a linear data structure that stores data sequentially based on the First In First Out (FIFO) manner and is also known as the First Come First Served data structure. However, in priority queue, the order of elements is based on their priority level. In this short article, we discussed how we can get the size of the priority queue in Python using various examples.

 

Further reading

Python queue
Python data structure

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment