Skip to content

Python Queue Methods Gottumukkala Sravan Kumar Spark By {Examples}

  • by

Python Queue methods are used to implement the queues very efficiently. The queue is a one-dimensional data structure which is also referred to as a FIFO (First In First Out) data structure. The element that is inserted at first will be the first element to retrieve.

Let’s understand what is Front and Rear in queues.

Front: It is the index where the first element is stored in the queue. Deletion takes place here.

Rear: It is the index where the last element is stored in the queue. Insertion takes place here.

In this article, we will discuss the methods supported by the Queue module, in order to use these methods you need to import the Queue module.

Insert elements one by one into queue using put() method

Return the total number of elements present in queue using qsize()

Check whether the queue is full or not using full() method

Popping elements from the queue using get() method

Check whether the queue is empty or not using empty()

1. Quick Examples of Queue methods

Let’s see the methods supported by Queue in python.

# Below are some quick examples.

# Initializing a queue with size 5
queue = queue.Queue(maxsize=5)

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

# Use the full() function to check queue is full or not.
print(queue.full())

# Use the get() function to delete element from queue in FIFO order
print(‘3 elements deleted from the queue are:’)
print(queue.get())
print(queue.get())
print(queue.get())

# Use the qsize() function return total number of elements
print(queue.qsize())

# Use the empty() function to check queue is empty or not.
print(queue.empty())

2. put() method of Queue

put() method is used to insert an item into the queue in Python. At a time, we can insert only one item. It takes an item to be inserted as a parameter.

2.1 Syntax for put() method

Let’s see the syntax of put() method.

# Syntax
queue.put(item)

Here, the queue is the input queue implemented using Queue(). and an item is the element you wanted to insert into the queue.

2.2 put() Example

Let’s create a queue with a max size of 5 and insert 5 countries one by one into it.

# Import
import queue

# Initializing a queue with size 10
queue = queue.Queue(maxsize=10)

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

3. get() method of Queue

get() method is used to remove only one item from the queue at a time. It won’t take any parameters. It will remove the firstly inserted item from the queue.

3.1 Syntax for get() method

Let’s see the syntax of get() method.

# Syntax
queue.get()

Here, the queue is the input queue object.

3.2 get() Example

Let’s create a queue with 5 elements and get 3 elements by deleting them.

import queue

# Initializing a queue with size 5
queue = queue.Queue(maxsize=5)

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

# Use the get() function to delete element from queue in FIFO order
print(‘3 elements deleted from the queue are:’)
print(queue.get())
print(queue.get())
print(queue.get())

# Output:
# 3 elements deleted from the queue are:
# China
# Russia
# England

We inserted the elements in the order – [‘China’, ‘Russia’, ‘England’, ‘Nepal’, ‘Italy’]. Now, we have deleted 3 elements one by one in order, so ‘China’, ‘Russia’, and ‘England’ were returned by the get() method in the same inserted order and these were deleted. Now the queue contains ‘Nepal’ and ‘Italy’.

4. qsize() method of Python Queue

The qsize() method will return the total number of elements present in the queue. It won’t takes any parameter.

4.1 Syntax for qsize() method

Let’s see the syntax of qsize() method.

# Syntax
queue.qsize()

4.2 qsize() Examples

Example 1: Let’s create a queue with 5 elements and return the total number of elements.

import queue

# Initializing a queue with size 5
queue = queue.Queue(maxsize=5)

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

# Use the qsize() function return total number of elements
print(queue.qsize())

# Output:
# 5

Example 2: Let’s delete 2 items and return the total number of elements present in the queue.

import queue

# Initializing a queue with size 5
queue = queue.Queue(maxsize=5)

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

# Use the qsize() function return total number of elements
print(queue.qsize())

queue.get()
queue.get()

# Use the qsize() function return total number of elements
print(queue.qsize())

# Output:
# 5
# 3

5. full() method of Python Queue

The full() method return True if the queue is full i.e if the total number of elements in the queue is equal to maxsize, Otherwise False is returned.

5.1 Syntax for full() method

Let’s see the syntax of full() method.

# Syntax
queue.full()

5.2 full() Example

Let’s create a queue with 5 elements and check whether it is full or not.

import queue

# Initializing a queue with size 5
queue = queue.Queue(maxsize=5)

# Use the full() function to check queue is full or not.
print(queue.full())

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

# Use the full() function to check queue is full or not.
print(queue.full())

# Output:
# False
# True

Firstly, the queue is not full. So False is returned. After that, we inserted 5 items into the queue. Now full() returned True (equal to maxsize).

6. empty() method of Python Queue

empty() method return True if the queue is empty. Otherwise False is returned.

6.1 Syntax for empty() method

Let’s see the syntax of the empty() method.

# Syntax
queue.empty()

6.2 Example

Let’s create a queue with 5 elements and check whether the queue is empty or not.

import queue

# Initializing a queue with size 5
queue = queue.Queue(maxsize=5)

# Use the empty() function to check queue is empty or not.
print(queue.empty())

# Using put() function to insert elements
queue.put(‘China’)
queue.put(‘Russia’)
queue.put(‘England’)
queue.put(‘Nepal’)
queue.put(‘Italy’)

# Use the empty() function to check queue is empty or not.
print(queue.empty())

# Output:
# True
# False

Firstly, the queue is empty. So True is returned. After that, we inserted 5 items into the queue. Now empty() returned False.

7. Conclusion

We have seen several Queue methods in Python first, learned how to insert elements to the queue using the put() method. To delete elements from the queue, we used the get() method. To check whether the queue is empty or not, we used full() and empty() methods. qsize() method will return the total number of elements present in the queue.

 Python Queue methods are used to implement the queues very efficiently. The queue is a one-dimensional data structure which is also referred to as a FIFO (First In First Out) data structure. The element that is inserted at first will be the first element to retrieve. Let’s understand what is Front and Rear in queues.  Read More Python, Python Tutorial, Python Queue Methods 

Leave a Reply

Your email address will not be published. Required fields are marked *