How to Implement a Queue in Python

The queue is a versatile data structure that you can use in a variety of settings. From CPU scheduling algorithms to web apps, it’s found everywhere.

If you’re striving to be a Python developer, it’s vital that you get up to speed with this simple yet omnipresent data structure. Let’s learn what the queue data structure is and how to implement one in Python.

4

What Is the Queue Data Structure?

A queue is a linear data structure, that follows the First-In-First-Out (FIFO) principle. This means that, when you fetch an element from a queue, you’ll get whichever one you added before the others.

Here are the basic operations that you may perform on a queue:

person holding python book

You can implement the queue data structure in Python in two ways: using either a list container or a doubly-ended queue from the collections module. For this program, you’ll use a list.

How to Implement the Queue Data Structure in Python

You’ll be implementing a queue using the list container in Python. Start by declaring an empty list with the namequeue.

Now you need to accept user input and perform the operation entered by the user. First, print out a line prompting the user to enter a command. Then, wait for user input and store it in thecommandvariable.

Illustration of a queue data structure

UsingPython if statements, perform the operation corresponding to the command the user enters. If they input an unrecognized command, quit the program. Put these operations inside an infinitewhile loopto ensure that the program keeps on running unless they quit.

Now you’ve dealt with the main control flow of the program, you can define the code block for each operation. First, write the code for enqueue. Enqueue means to insert an element at the end of the queue. You can do this using theappend()method:

Gray laptop showing html code

Now, write the code to dequeue an element from the queue. You can do this using the pop method with 0 as the index. Why? As you learned earlier, a queue follows FIFO order, so the first element you enqueue should be the first element you dequeue.

Moving on, write the code to print the element at the front of the queue. Simply print out the 0th index of the queue.

Person holding the Google Pixel 9a showing the back of the phone

Much like the code for the front operation, to perform the rear operation print out the element at the last index. To do this first use the len() function on the queue and then subtract 1 from it to find the last index.

Finally, write the code for the print command. Simply print out the list using Python’s standardprint()function.

If the text the user enters does not match a supported command, break out of the while loop using a break statement. The final code should look like this:

Run the program to try out the various queue operations. Use the print command to see how they affect your queue. You’ve now created your own simple queue implementation in Python.

The Queue Is Just One of Many Useful Data Structures

The concept of a data structure is a vital one that every computer science student must master. Chances are you may have already learned or worked with some basic data structures such as arrays or lists.

Interviewers also tend to ask questions related to data structures, so if you’re looking to bag a high-paying programming job, you’re going to need to brush up on your data structures knowledge.

Get a leg up on the competition by learning these heaps and trees.

Flagship price, mid-range phone.

It saves me hours and keeps my sanity intact.

Your phone is a better editor than you give it credit for.

Who asked for these upgrades?

Turn these settings on, and your iPhone will be so much better than before.

Technology Explained

PC & Mobile