What is Queue in Python? Explain with examples

PythonServer Side ProgrammingProgramming

A queue is a linear data structure that works on the First In First Out mechanism(FIFO).

The element which enters first in the queue is the first to be processed.

Example

The queue data structure can be understood with the help of a queue at a bus stand. The person who reaches first at the bus stand is the first person in the queue and other persons stand him as they reach the bus stand. When the bus arrives, the person who reached first at the bus stand will be the first person to enter the bus and the rest will enter in the order in which they reached the bus stand. Thus, the FIRST IN FIRST OUT mechanism is followed.

Implementation of Queue in Python

The Queue in Python can be implemented in various ways using other linear data structures or in-built modules in the Python library.

Method 1 − Implement using list

The queue in Python can be implemented using list. It is not very efficient since inserting or deleting an element at the beginning of the list takes O(n) time which is slow compared to an implementation using other ways.

Operations involved

append() − This function adds an element at the end of the queue.

pop(0) − This function removes and returns the first element in the queue.

Example

 Live Demo

queue=[]
queue.append(1)
queue.append(2)
queue.append(3)
print("Initial queue",queue)
print("Element popped from the queue")
print(queue.pop(0))
print(queue.pop(0))
print("Queue after popping some elements",queue)

Output

Initial queue [1, 2, 3]
Element popped from the queue
1
2
Queue after popping some elements [3]

You can’t remove more elements once the queue is empty. Doing so results in an exception.

queue.pop(0)
IndexError: pop from empty list

Method 2 − Implement using queue.Queue

This is the way to implement queue using inbuilt module from python. We need to import Queue from queue. We can initialize queue with some specific size. A size of zero means an infinite queue.

Operations involved

maxsize − maximum number of elements allowed in a queue

get() − remove and return the first element from the queue. If queue is empty, wait until queue has atleast one element.

get_nowait() − remove and return the first element from the queue.If queue is empty, raise an exception.

put(item) − append an element at the end of the queue.If queue is full, wait until a free slot is availabe.

put_nowait(item) − append an element at the end of the queue.If queue is full, raise an exception.

full() − returns true if the queue is full, else return false.

empty() − return True if the queue is empty, else false

qsize() − returns the number of elements present in the queue

Example

 Live Demo

from queue import Queue
q=Queue(maxsize=3)
q.put(1)
q.put(2)
q.put(3)
print("Is queue full",q.full())
print("Element popped from the queue")
print(q.get())
print(q.get())
print("Number of elements in queue",q.qsize())
print("Is queue empty",q.empty())

Output

Is queue full True
Element popped from the queue
1
2
Number of elements in queue 1
Is queue empty False

Method 3 − Implement using collections.deque

This is another way to implement a queue in Python. We need to import deque from the collections module.

Operations involved

append() − This function adds an element at the end of the queue.

popleft() − This function removes and returns the first element in the queue in O(1) time complexity.

Example

 Live Demo

from collections import deque
queue=deque()
queue.append(1)
queue.append(2)
queue.append(3)
print("Intial queue: ",queue)
print("Element popped from the queue")
print(queue.popleft())
print(queue.popleft())
print("Queue after popping some elements: ",queue)

Output

Intial queue: deque([1, 2, 3])
Element popped from the queue
1
2
Queue after popping some elements: deque([3])

Using popleft() function on an empty deque will raise an exception.

raja
Published on 11-Mar-2021 09:17:22
Advertisements