In this Python tutorial, you will learn:

What is Python Queue? How does Python Queue Work?
Types of Queue in Python
Python queue Installation
Methods available inside Queue and LifoQueue class
First In First Out Queue Example
Last In First Out queue Example
Add more than 1 item in a Queue
Sorting Queue
Reversing Queue

How does Python Queue Work?

The queue can be easily compared with the real-world example the line of people waiting in a queue at the ticket counter, the person standing first will get the ticket first, followed by the next person and so on. The same logic goes for the queue data structure too. Here is a diagrammatic representation of queue:

The Rear represents the point where the items are inserted inside the queue. In this example, 7 is value for that. The Front represents the point where the items from the queue will be removed. If you remove an item from the queue, the first element you will get is 1, as shown in the figure. Item 1 was the first one to be inserted in the queue, and while removing it is the first one to come out. Hence the queue is called FIRST IN FIRST OUT (FIFO)

In a queue, the items are removed in order and cannot be removed from in between. You just cannot remove the item 5 randomly from the queue, to do that you will have to remove all the items before 5. The items in queue will be removed in the order they are inserted.

Types of Queue in Python

There are mainly two types of queue in Python:

First in First out Queue: For this, the element that goes first will be the first to come out.To work with FIFO, you have to call Queue() class from queue module. Last in First out Queue: Over here, the element that is entered last will be the first to come out.To work with LIFO, you have to call LifoQueue() class from the queue module.

Python queue Installation

It is very easy to work with queue in python. Here are the steps to follow to make use of queue in your code. Step 1) You just have to import the queue module, as shown below: The module is available by default with python, and you don’t need any additional installation to start working with the queue. There are 2 types of queue FIFO (first in first out) and LIFO (last in first out). Step 2) To work with FIFO queue , call the Queue class using the queue module imported as shown below: Step 3) To work with LIFO queue call the LifoQueue() class as shown below:

Methods available inside Queue and LifoQueue class

Following are the important methods available inside Queue and LifoQueue class:

put(item): This will put the item inside the queue. get(): This will return you an item from the queue. empty(): It will return true if the queue is empty and false if items are present. qsize(): returns the size of the queue. full(): returns true if the queue is full, otherwise false.

First In First Out Queue Example

In the case of first in first out, the element that goes first will be the first to come out.

Add and item in a queue

Let us work on an example to add an item in a queue. To start working with the queue, first import the module queue, as shown in the example below. To add an item , you can make use of put() method as shown in the example: By default, the size of the queue is infinite and you can add any number of items to it. In case you want to define the size of the queue the same can be done as follows Output: Now the size of the queue is 5, and it will not take more than 5 items, and the method q1.full() will return true. Adding any more items will not execute the code any further.

Remove an item from the queue

To remove an item from the queue, you can use the method called get(). This method allows items from the queue when called. The following example shows how to remove an item from the queue. Output:

Last In First Out queue Example

In the case of last in the first out queue, the element that is entered last will be the first to come out. To work with LIFO, i.e., last in the first out queue, we need to import the queue module and make use of the LifoQueue() method.

Add and item in a queue

Here we will understand how to add an item to the LIFO queue. You have to use the put() method on LifoQueue, as shown in the above example.

Remove an item from the queue

To remove an item from the LIFOqueue you can make use of get() method .

Output:

Add more than 1 item in a Queue

In the above examples, we have seen how to add a single item and remove the item for FIFO and LIFOqueue. Now we will see how to add more than one item and also remove it.

Add and item in a FIFOqueue

Remove an item from the FIFOqueue

Output:

Add and item in a LIFOqueue

Remove an item from the LIFOqueue

Output:

Sorting Queue

Following example shows the queue sorting.The algorithm used for sorting is bubble sort. Output:

Reversing Queue

To reverse the queue, you can make use of another queue and recursion. The following example shows how to get the queue reversed. Example: Output:

Summary:

A queue is a container that holds data. There are two types of Queue, FIFO, and LIFO. For a FIFO (First in First out Queue), the element that goes first will be the first to come out. For a LIFO (Last in First out Queue), the element that is entered last will be the first to come out. An item in a queue is added using the put(item) method. To remove an item, get() method is used.