Python queue

Filed Under: Python

In our previous tutorial we have discussed about python time sleep function of the time module. In this tutorial we will learn about python queue module.

Python queue

When we want to process our data in the way they have arrived, then using queue is the best option. Queue is a concept of first in first out data structure. Have a look at the following example:


# importing the queue module
import queue

# taking an object of Queue()
q = queue.Queue()

# enqueueing some value in the object of Queue
q.put('A')
q.put('B')
q.put('C')
q.put('D')
q.put('E')

# retrieving the values of the Queue
for i in range(q.qsize()):
   print(q.get())

This will output:
python queue

Python Queue Example code analysis

In the very first line we have imported the python queue module. Then we have created an object of Queue as the queue module contain three classes like Queue, LifoQueue and PriorityQueue.

Then we have added some value to the object of queue using put() method. Finally we have retrieved the values until the size of the q, using get() method. And if you notice the output, you will see that the outputs are as the way we have added.

Python Queue common methods

In the python queue module, the followings are the most common methods that are used to manipulate or operate on the queue objects. We have already seen three methods in the previous example. They are put(), get() and qsize(). We will see some other methods.

Python Queue full() function

To check whether a queue object is full or not. It returns true if the queue is full otherwise false.

Python Queue empty() function

If the queue is empty then it returns true otherwise false.

See the following example:


# importing only the Queue from the queue module
from queue import Queue

# taking an object of Queue()
q = Queue()
print("Initially the size of queue is %s" % q.qsize())
print("Checking whether queue is empty or not. Empty?? = %s" % q.empty())

# enqueueing some value in the object of Queue
q.put('A')
q.put('B')
q.put('C')
q.put('D')
q.put('E')

print("After adding some value, size of queue is %s" % q.qsize())
print("Checking whether queue is full or not. Full?? = %s" % q.full())

# retrieving the values of the Queue
for i in range(q.qsize()):
   print("Retrieved = ", end=' ')
   print(q.get())

# after retrieving, check the size of the object
print("Size of queue is = %s " % q.qsize())

Above python queue example code will produce following output.


Initially the size of queue is 0
Checking whether queue is empty or not. Empty?? = True
After adding some value, size of queue is 5
Checking whether queue is full or not. Full?? = False
Retrieved =  A
Retrieved =  B
Retrieved =  C
Retrieved =  D
Retrieved =  E
Size of queue is = 0 

Python LifoQueue

Python LifeQueue is similar to queue except that it’s last-in-first-out data structure. Below is a simple python LifoQueue example code.


import queue

q = queue.LifoQueue()

q.put('A')
q.put('B')
q.put('C')

for i in range(q.qsize()):
    print(q.get())

Below image shows the output produced by python LifoQueue example code.

python lifoqueue example

Python Priority Queue

Priority Queue returns the item in the order of the priority. So when we add an item to the priority queue, we also provide it’s priority. Then we retrieve items, they are returned in the order of the priority. Lower number priority items are returned first.

Below is python PriorityQueue example code.


import queue

q = queue.PriorityQueue()

q.put((1, 'A'))
q.put((3, 'B'))
q.put((2, 'C'))

for i in range(q.qsize()):
    print(q.get())

Here is the output produced by python priority queue example program.

python priority queue example

Priority Queue Python heapq module

Python Queue module PriorityQueue functions are synchronized. There is another module heapq that also implements priority queue in python.


import heapq

q = []

heapq.heappush(q, (2, 'B'))
heapq.heappush(q, (1, 'A'))
heapq.heappush(q, (3, 'C'))

while q:
    item = heapq.heappop(q)
    print(item)

Output produced by heapq priority queue implementation example program is shown in below image.

python heapq example, priority queue python

That’s all about Python Queue, Priority Queue and LifoQueue.

Reference: Official Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages