Python Queue

There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for your turn in some store, we all are pretty familiar with the concept of the Queue as we wait for our services. In the same way, the queue in data structure plays a significant role in data science and computer engineering. The thing which makes the Queue unique is its functionalities. Based on the FIFO (First-In-First-Out) model, the data elements are allowed at one end and removed from the other one. But before we jump into the depth of the Queue, let’s discuss – What do we understand by the Queue in Python?

Queue in Python

The Queue in Python is an important concept in Data Structure. The Queue is a data structure where the data elements are organized in a linear manner. Moreover, the Queue in Python helps in controlling the flow of the tasks.

Like the Stack, the Queue is a linear data structure where the data elements are stored with the FIFO (First-In-First-Out) Principle. Its property implies that the least recently added data elements would be removed first. Let’s take a real-life example, where few customers are standing in Queue at the bill counter and wait for their turn to come. The shopkeeper is dealing with each customer one after one. Thus, who comes first would be served first.

Now, let’s discuss some operations associated with the Queue data structure. Have a look at a schematic diagram representing a Queue shown below:

Python Queue

Some of the operations associated with Queue and their descriptions are listed as follow:

S. No.OperationDescription
1FrontThe Front returns the first data element from the Queue – Time Complexity: O(1)
2RearThe Rear returns the last data element from the Queue – Time Complexity: O(1)
3EnqueueThe Enqueue is used to add the data element to the Queue. However, the condition is said to be overflowing if the Queue appears to be full – Time Complexity: O(1)
4DequeueThe Dequeue is used to remove the data element from the Queue. The data elements in the Queue are popped out in the same manner as they as pushed in. However, the condition is said to be underflowing if the Queue appears to be empty – Time Complexity: O(1)

Queue Implementation in Python

Python provides several methods for implementing a Queue. In this section, we will discuss the data structures and modules available in the Python library that are useful in implementing a Queue. Some of the methods used in Queue Implementation are given below:

  1. list
  2. queue.Queue
  3. collections.deque

Queue Implementation using List

The list is a built-in Python data structure available that can help in implementing a Queue. The list uses the functions like append() and pop() instead of enqueue() and dequeue() respectively. However, the list is quite slow to fulfill the purpose as the insertion or removal of a data element at the start needs a shift in all the other data elements by one, requiring the O(n) time.

Here is an example given below that illustrates the Queue’s implementation using list

Output:

Explanation:

In the above code, we have initialized a list as the Queue and inserted a few data elements such as apple, banana, mango and orange in it with the append() function. Then, we have used the pop() function specifying the data element’s zeroth position we wanted to remove.

Thus, the above list is behaving like a Queue. Moreover, in the last line of code, we have again used the pop() function on an empty queue that will return an Index Error stating the list is empty.

Queue Implementation using Classes

Using the collections.deque Class

The deque class in Python is used for implementing a double-ended queue supporting the insertion and elimination of data elements from either end in Time Complexity: O(1) (non-amortized).

We can implement a Queue in Python with the help of the deque class provided by the collections module. Since the deque class provides a quicker insert and remove operations, it becomes preferable over the list. However, the deque class uses append(), and popleft() functions instead of the enqueue and deque. Let’s see an example based on this class.

Output:

Explanation:

In the above example, we have imported the deque class from the collections module and initialize a queue using the class. We have then used the append() function to insert the data elements such as Apple, Mango, Banana, Orange, and Kiwi into the queue. Once the queue is created, we have used the popleft() function to remove data elements one by one in the systematic queue order. However, the function will return an error if the queue gets empty, as in the last example.

Using the queue.Queue class

Python provides a built-in queue module that provides access to its Queue class and implements a queue. The queue.Queue(maxsize) class is used to initialize a variable to a maximum size of maxsize. If maxsize is defined as zero ‘0’, it will create an infinite queue. Moreover, this Queue is based on the FIFO principle.

Some of the functions used in this module are listed in the following table:

S. No.FunctionDescription
1maxsize()The maxsize() function is used to define the number of items that are allowed in the queue.
2full()The full() function returns a Boolean value depending on whether the queue is full or not. It returns True if the data elements are equal to the maxsize of the queue. However, it never returns True if the queue is initialized with the maxsize = 0 (i.e., default).
3empty()The empty() function also returns a Boolean value depending on whether the queue is empty or not. Thus, it returns True if the queue is empty or otherwise False.
4put()The put() function is used to insert an item or data element into the queue. However, it waits until a free slot is available if in case the queue is full.
5get()The get() function is used to remove and return an item or data element from the queue. However, it waits for the item until it’s available if in case the queue is empty.
6put_nowait(item)The put_nowait() function is used to insert an item or data element into the queue without blocking or waiting. Moreover, it returns QueueFull if there is no free slot available immediately in the queue.
7get_nowait()The get_nowait() function is used to remove and return an item or data element from the queue without blocking or waiting. Moreover, it returns QueueEmpty if in case the queue is empty.
8qsize()The qsize() function is used to return the total number of items available in the queue.

Let’s see an example based on this class.

Output:

Pin It on Pinterest

Share This