How to use Python queue – with example

  • Reading time:17 mins read

What is the Python queue data structure used for?

Python queue is a linear abstract data structure that follows the first in-first out (FIFO) approach. This means that the first item in the queue is the first item to be removed while the most recent entry into the queue is the last item to be removed from the queue.

 

Okay, but what does a Python queue look like?

A Python queue data structure is similar to a real-life queue as seen in a grocery store where the first customer on the queue is served first while the last customer on the same queue will be served last. Unlike lists where we can remove items from the middle of the list in queues, it is much harder to remove items from the middle of the queue.

 

python queue

 

Applications of Python queue Data Structure

Some of the most common applications of the queue data structure include:

  • Handling real-time system interrupts
  • Routing and switches in networking
  • CPU and disk scheduling
  • Managing calls in call center systems by holding calls in the order of their calling.

Implementing the Python queue data structure

There are various ways that we can use to implement the queue data structure. These include:

  • Implementation using a list.
  • Implementation using the collections.deque class
  • Implementation using the queue module.

In this article, we will only learn how we can implement the queue data structure using the built-in module known as a queue.

 

python queue

 

Getting started with the Python queue module

We will begin by importing this module at the top of our program using the import keyword. We will then proceed to initialize a variable q that will be our queue using queue.Queue(maxsize). The max size argument specifies the number of items that the queue can hold when not specified or when equated to zero the queue can hold an infinite number of items. 

The queue module provides us with a range of functions that we can use to perform various operations on the queue these include:

  • put() – Allows us to add items to the queue.
  • get() – Removes and returns an item from the queue.
  • qsize() – Returns the number of items in the queue.
  • empty() – Returns True if the queue has no items
  • full() – Returns True if the max size of the queue has been reached.
  • put_nowait() – Add an item to the queue if there is an empty slot returns an error if otherwise

Using the queue that we created earlier on named q we are going to explore how we can use each of the above methods.

Adding items to a Python queue

We will begin by adding items to the queue using the put() function. Using the qsize() function we can confirm the number of items in a queue as shown below.

 

Using the empty() function

We can also see if the queue is empty or not using the empty() function. This function returns True if the queue is empty and False if otherwise.

 

Using the full() function

In this case, since we have not specified the maximum size of the queue that means that the full()  function will always return False since the size of the queue is infinite.

 

Remove items from a Python queue

We can also remove and return an item from the queue using the get() function. Since this function also returns the removed item we can also print it out.

Now that we have removed one item from the queue you can see that the size of the queue has been reduced to one. In this case, we have removed the lion, the first item on the queue.

 

Example code

Now suppose we also remove the remaining item from the queue. In that case, we expect that the queue will be empty and therefore the empty() function should return True.

 

Using the maxsize argument

In the prior examples, the queue has been of infinite size. However, we can also set the maximum number of elements that a queue can hold using the maxsize argument.  For instance, we can set a maxsize of three and add another item to the queue.

 

Using the put_nowait() function

In this case, the queue is full therefore, we cannot add new elements to the queue. In this scenario where we are not sure if there is any space in the queue, we are going to use the put_nowait() function. 

This function allows us to add an item to the queue without blocking and returns an error if space is not immediately available in the queue.

 

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.