Python Queue Example : Implementation, Examples and Types

Here is another tutorial about Python Queues. In this post Python Queue Example we will learn about Queue in Python . We will try to learn types of Queues in python and how we can implement it.

What is Queue?

So, the first question arises that what is queue? Queue is an abstract data structure which is opens at both its end. Where one end is always used to insert data (this operation is also called as (enqueue) and other end is used for removal of data (i.e. dequeue).

In python a queue can be implemented using Lists where we can use the insert() and pop() methods to add and remove elements. There is no insertion as data elements are always added at the end of the queue.

So in this Python Queue Example, we will learn about implementation of FIFO queue in python using lists and also learn about Deque (Double-ended queue) and priority queue.

Creating a Queue in Python

We can create a queue by importing the Queue class. When you create a queue in python you can think it as of Lists that can grow and Shrink.

Adding Elements to a queue

Now we will create a queue class and will try to implement the FIFO technique. We will use built-in function insert() for adding the values in queue. I have already discussed about insert() method in my previous tutorial Python Tuple vs List.

In Example given above we have created a Queue MySuperHero and added 5 values to this Queue and finally get the size of the queue.

Removing elements from queue

For extracting the data from queue we will use pop() method.

when the above code is executed, it will show:

Thor
Iron man

In the example above we have made a method removefromQueue() for removing the data from queue.

Deque

A double-ended queue, or deque, has the feature of adding and removing elements from both end.  Now we will try to implement deque via following example:

when the above code is executed it will produce following output:

In the above example we have created a double ended queue and initially it has three values. After that we have are using append() method to add values to both the end simultaneously.

Priority Queue

Priority Queue is an extension of queue with following properties.

  1. Every item has a priority associated with it.
  2. An element with high priority is get service before an element with low priority.
  3. If two elements have the same priority, they are served according to their order in the queue.

The functionality of priority queue is same in python as in another language. The only difference lies in the implementation of it. So will understand it with an example.

We have taken a simplest example to understand the priority queue. In the above code we have made a priority queue pq and passed a tuple in queue also assigned three values to it with its priority. The priority varies from number 1 to 10 . Lowest no means highest priority and vice versa.

When the above code is executed it will produce following output:

Points to remember

  1. In python we can create a Queue using Lists which can grow and shrink.
  2. We can use insert(), append() method for inserting the values in queue and pop() method for extracting the values from Queue.
  3. Python supports FIFO,LIFO, Deque and priority Queue implementation.

So that is all for this post, if you are having any queries regarding this Python Queue Example, then you can leave in comment section below. Thank You 🙂

Leave a Comment