What is Python Queue?
Python queue is an important concept in data structure. Queue in Python is nothing but data item containers. With the help of queue in Python, we can control the flow of our tasks.
Say, we are manipulating data that are collected from a website and then writing the manipulated data into a .txt file. Now, if we have not collected the data from the website, we would not be able to start the data manipulating task, right? We don’t want to save the data right after collecting it from the website to avoid messing up the workflow of the whole task. Python queue is a great way to keep things in track.
- First-in First-out Queue
- Last-in First-out Queue
- Priority Queue
Let’s see what this module has to offer:
So, let’s get started.
Interested in learning Python? Check out the Python Training in Sydney!
Python First-in First-out Queue
As the name suggests, in the first-in first-out queue policy, the first element that goes in comes out first. It is just like putting the element inside an open cylinder. Before we move ahead, we need to import one library called queue. After that, we can create instances which can be viewed as containers.
Let us see how to do that.
import queue q = queue.Queue()
This chunk of code will import the queue library and will create one instance of queue. By default, this queue in python is of type first-in first-out (FIFO type). In case of the FIFO queue, the item that we enter first gets out first.
Watch this Python Numpy Tutorial Video for Beginners:
Adding an item in a queue:
We can add an item into a queue in Python with the help of the put function. This is as simple as it sounds. Let’s try that with a Python Queue example.
import queue q = queue.Queue() q.put(5)
This will create an instance called q where we will be adding 5.
Removing an item from a queue in Python:
To remove items from a queue in Python, we will be using the get function. See the Python Queue example given below.
import queue q = queue.Queue() q.put(9) print(q.get()) #to print the item that we are removing Output:9 print(q.empty())
To make sure that our queue is empty, we can use the empty function which will return Boolean values.
Adding more than one item into a queue in Python:
The real application of a queue is when there is more than one item to deal with. Let us dive into one Python Queue example where we will be adding more than one item using the FIFO method.
import queue q = queue.Queue() for i in range(9): q.put(i)
This chunk of code will create one queue instance and then will add items ranging from 0 to 8.
Removing more than one item from a queue in Python:
Now that we have added items into the queue, let us see how to get them out of it.
import queue q = queue.Queue() for i in range(9): q.put(i) while not q.empty(): print(q.get(), end=” “) print(“FIFO”) Output: 0 FIFO 1 FIFO 2 FIFO 3 FIFO 4 FIFO 5 FIFO 6 FIFO 7 FIFO 8 FIFO
Become a master of Python by going through this online Python Course in Toronto!
Python Last-in First-out Queue
Again, as the name says, in this type of queues, the element that goes in first comes out last. In other words, the one that goes at last comes out first. But unlike FIFO (which is considered as the default queue type), we have to add a special function to operate LIFO queues.
Let us explore the LIFO queue with the help of an example:
import queue q = queue.LifoQueue()
Adding an item into a queue:
Similar to what we did while understanding the FIFO type, let us add 5 to a queue.
import queue q = queue.LifoQueue() q.put(5)
Removing an item from a queue:
Use the same function to remove the item from the queue.
import queue q = queue.LifoQueue() q.put(5) print(q.get())
But, the concept of LIFO is not visible here, isn’t it? Let us add more than one item into the queue and see which one gets out first.
Adding more than one item into a queue:
Let us add numbers from 0 to 8 into a queue.
import queue q = queue.LifoQueue() for i in range(9): q.put(i)
Removing an item from a queue:
Now that we have added items into the queue, let us see which one gets out first.
import queue q = queue.LifoQueue() for i in range(9): q.put(i) while not q.empty(): print(q.get(), end=” “) print(“LIFO”) Output: 8 LIFO 7 LIFO 6 LIFO 5 LIFO 4 LIFO 3 LIFO 2 LIFO 1 LIFO 0 LIFO
Get certified from this top Python Course in Singapore today!
Priority Queue in Python
The priority queue in Python is a bit interesting one. Here, the order in which we put the items in doesn’t matter. What really matters is the value of the items. But, how is that going to work? Well, the logic behind this policy is that the lowest valued item gets out first.
Again, to be able to perform priority queue operations, we have to use another function shown below.
import queue q= queue.PriorityQueue()
Let us see how this works with the help of an example.
Let us add 5 elements into a queue using the priority queue function.
import queue q= queue.PriorityQueue() q.put(2) q.put(4) q.put(1) q.put(0) q.put(2)
Watch this Text Mining in Python Tutorial Video for Beginners:
Now that we have the items in the queue, let us use the Python for loop to remove the items. We will be using the q.qsize() function (returns the size of the queue) in order to run the for loop function.
import queue q= queue.PriorityQueue() q.put(2) q.put(4) q.put(1) q.put(0) q.put(2) for i in range(q.qsize()): print(q.get(i)) output: 1 2 2 4
As we can see here, the item with the lowest value gets out first. But again, if two elements are of the same value, then the order is taken into consideration while removing the elements out of the Python queue.
Further, check out our Python interview questions by experts. Big companies like Netflix and IBM use Python. Dropbox is created in Python, and hundreds of other big companies are also adapting Python. So, this is not a surprise that Python has become one of the fastest-growing programming languages according to Stack Overflow. The ever-expanding applications of large-scale Data Science and Artificial Intelligence have become two of the most aspiring fields in the 21st century. To be a part of these competitive and most lucrative fields, one must fulfill the requirement of having a well-rounded understanding of Python.
Further, Intellipaat’s industry-designed Python course will help you get a more elaborate understanding of Python.