Home » Python Tutorial » Python Queue with Examples

Python Queue with Examples

A python queue is a linear data structure like a stack. It follows the last in first out (FIFO) manner. That means that the first element entered in the queue leaves the queue first. The best example of the queue we experience in day to day life is who comes first serves first. 

The queue has two ends, the rear end from where the elements are added into the queue and the front end from where elements are removed from the queue. To add an element in the queue, we use put() this is known as enqueue operation, and remove the element we use get() known as dequeue operation.

It also provides full and empty functions. Since python used dynamic memory allocation, these conditions will never get fulfilled. To use these functions, we need to set max size in the queue then only use empty(), and full() functions.

How to implement a python queue?

There are four ways to implement the queue.

1. Without using any predefined class

Example 1

q = []

q.append('a')
q.append('b')
q.append('c')

print(q)

x = q.pop(0)
print('removing ',x,' from the queue')
y = q.pop(0)
print('removing ',y,' from the queue')
z = q.pop(0)
print('removing ',z,' from the queue')

Output

['a', 'b', 'c']
removing  a  from the queue
removing  b  from the queue
removing  c  from the queue

In the above example,  we can see the element added first in the queue get priority to leave the queue first. 

Example 2

We can count the elements in the queue by using the length function in python. The length function will return the count of the element present in the queue.

q = []

for i in range(10):
   q.append(i)

print(q)
print('The number of elements present in the queue is:',len(q))

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The number of elements present in the queue is: 10

In the above example, we use the length function to count the number of elements present in the queue. So there are 10 elements present in the queue.

2. Using a Queue class to implement a python queue

Example 1

import queue

q = queue.Queue()

q.put('a')
q.put('b')
q.put('c')
q.put('d')

print(q)

print('Removing elements from queue')
print(q.get())
print(q.get())
print(q.get())
print(q.get())

Output

<queue.Queue object at 0x00000243BE3B0F08>
Removing elements from queue
a
b
c
d

You can see, we can not print the object of the queue class directly in the print function. We need to iterate the queue in the loop and fetch each element individually to print it.

Example 2

import queue

q = queue.Queue()

q.put('a')
q.put('b')
q.put('c')
q.put('d')


print('Elements present in the queue are :')
while a.qsize():
   print(q.get())

Output

a
b
c
d

Example 3

We know that the queue follows the First in first out manner. But, python provides a LIFO queue to print the queue in contrast to its behavior. Let’s see how to implement it.

import queue

q = queue.LifoQueue()

q.put('a')
q.put('b')
q.put('c')
q.put('d')


print('Removing elements from the queue')
print(q.get())
print(q.get())
print(q.get())
print(q.get())

Output

Removing elements from the queue
d
c
b
a

In the above example, you can see the sequence of removing elements is different. LIFO queue removes the last added element first and so on.

3. Implementing the deque in the python

Deque is nothing but a queue in which the elements are added and removed from both ends. Deque stands for double-ended queue. It provides a few functions like append(), appendleft(). These functions are used to add elements to the queue. The append() will add elements from the front end and appendleft() will add the element from the rear end. Also, deque provides functions to remove an element from the queue. pop() function will remove an element from the front end and popleft() will remove an element from rear end().

Example

from collections import deque

q = deque()

for i in range(7):
   q.append(i)

for i in range(7,15):
   q.appendleft(i)

print(q)

print('removing {} from left of the queue'.format(q.popleft()))
print('removing {} from right of the queue'.format(q.pop()))
print('removing {} from left of the queue'.format(q.popleft()))
print('removing {} from right of the queue'.format(q.pop()))

print('Final result is :',q)

Output

deque([14, 13, 12, 11, 10, 9, 8, 7, 0, 1, 2, 3, 4, 5, 6])
removing 14 from left of the queue
removing 6 from right of the queue
removing 13 from left of the queue
removing 5 from right of the queue
Final result is : deque([12, 11, 10, 9, 8, 7, 0, 1, 2, 3, 4])

4. Implementing a python priority queue.

Priority queue python uses the heapq model to sort the elements of the queue when the user adds an element into the queue. While popping elements the highest priority element gets removed first from the queue.

from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'a'))
q.put((1, 'b'))
q.put((3, 'c'))

print(q.get())
print(q.get())
print(q.get())

Output

(1, 'b')
(2, 'a')
(3, 'c')

FAQ’s

How to use a queue in python?

To use a queue there are predefined functions for adding elements and removing elements from the queue. You can use them for implementing queues.

How to import queue in python?

Syntax to import queue in python

import queue
q = queue.Queue()

How to implement queue in python?

You can use the list as a queue by using its functions or you can simply add a queue class and used its objects to implement the queue in python.

Did you know?
1. Absolute Value in Python
2. Leap Year Program in Python
3. Factorial Program in Python
4. Python Split with Examples
5. Python Substring
6. Features of Python
7. Python Square Root
8. Python Round() Function
9. Python Strip
10. Python Queue

Pin It on Pinterest