bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Python Queue 

Python Queue is an important concept in Data Structure. Queues are nothing but data item containers. With the help of queues, you can control the flow of your tasks.

Say, you are manipulating data that are collected from a website and then writing the manipulated data into a .txt file. Now imagine if you have not collected the data from the website you won’t be able to start the data manipulating task, right? You also don’t want to save the data right after collecting it from the website. In other words, you don’t want to mess the workflow of the whole task. Python Queue is a great way to keep things in track.

There are three types of queues in Python. They are:
1. First In First Out
2. Last In First Out
3. Priority Queue

Let’s see what this module has to offer in case you want to jump right into one specific concept.

So, Let’s get started.

Python First In First Out Queue

As the name suggests in 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. So, 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 queue library and will create one instance of queue. By default, this queue is type First In First Out. In case of FIFO queue, the item that we enter first gets out first.

1. Adding an item in a queue:
Add an item into a queue with the help of put function. This is as simple as it sounds. Let’s try that with an example.

import queue
q = queue.Queue()
q.put(5)

This will create an instance called q where we will be adding 5.

2. Removing an item from a queue:
To remove items from a queue we will be using get function. See the example given below.

import queue
q = queue.Queue()
q.put(9)
print(q.get())  #to print the item that we are removingOutput:9
print(q.empty())

To make sure that our queue is empty we can use empty function which will give Boolean values.
Output:

True

3. Adding more than one item into a queue:
The real application of queue is useful when there is more than one item to deal with. Let us dive into one example where we will be adding more than one item using 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.

4. Removing more than one item from a queue:
Now that we have added the items into that queue, let us see how to get them out.

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

Data science masters program

Python Last In First Out Queue

Again, as the name itself tells you, in this type of queue, the element that goes in first comes out at last or in other words the one that goes, at last, comes out first. But unlike FIFO(which is considered as the default queue), you have to add a special function to operate LIFO queue.
Let us explore that with the help of an example:

import queue
q = queue.LifoQueue()

1. Adding an item into a queue:
Similar to FIFO, let us add 5 to a queue.

import queue
q = queue.LifoQueue()
q.put(5)

2. Removing an item from a queue:
Use the same function to remove item from 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 and see which one gets out first.

3. 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)

4. 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

Python Priority Queue

The priority queue is bit interesting one. The order you put them in doesn’t matter here. What really matters is the value of the item. But you must be wondering how is that going to work, right? Well, the logic behind this policy is that the lowest valued item gets out first.
Again, to be able to perform priority queue operations you have to use another function shown below.import queue
q= queue.PriorityQueue()Let us see how this works with the help of an example.
Example:
Let us add 5 elements into a queue using priority queue function.

import queue
q= queue.PriorityQueue()
q.put(2)
q.put(4)
q.put(1)
q.put(0)
q.put(2)

Now that we have the items in the queue, let us use for loop to remove the items. We will be using q.qsize() function (returns the size of the queue), in order to run the for loop.

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:

0
1
2
2
4

As you can see here, the item with lowest value gets out first. But again, if two elements are of same value then the order is taken into consideration while removing the elements out of the queue.

Conclusion

This brings us to the end of this tutorial, where we have gone through the Fundamentals of Python, control flow statements, Loops and functions. We have also discussed different File Handling, Exception Handling methods. By the end of this tutorial, we have also gathered an idea about how to work with different modules. Further, you can check our free Python interview questions by experts. Big companies like NetflixIBM use Python, Dropbox is also created in Python and hundreds of other big companies are also adapting Python. So, this is no 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 field in the 21st century.To be part of these competitive yet two of the most lucrative fields, one must fulfill the requirement of having a well-rounded understanding of Python. This structured Python training course will help you get an elaborate understanding of Python.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python Queue"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Queue.