Python Queue

Python provides many built-in modules, functions, and statements that help the programmers to perform various complicated tasks easily. It also provides many built-in data structures like lists, tuple, and dictionaries, which ensure the efficient creation and management of data in applications. A queue is a data structure that stores and manages the data. It stores the data in a first-in, first-out (FIFO) order. The element that is inserted first will be removed first. We can understand the working of the queue from our daily life example. It‘s like a queue of customers; the customer who comes first is facilitated first. In Python, we can create the queue in the following ways:

  • List
  • queue.Queue
  • collections.deque

This article explains the Python Queue in detail with Example.

Common Queue Operations:

There are many queue related operations. However, these are the 4 common queue operations:

  1. Enqueue
  2. Dequeue
  3. Front
  4. Rear

The enqueue operation is performed to add an item to the queue, whereas the dequeue removes the items from the queue in the same order as they were inserted. Additionally, the front operation returns the first, and the rear operation returns the last item of the queue.

Queue implementation using the list

A list is a widely used Python built-in data structure that arranges the items in sequence. With the list, we can use the append() and pop() functions, which are used to insert and remove items from the queue, respectively. The append() function adds the item at the end of the list. The pop function takes the index as an argument and removes the item from the list.  We can store items of any type in the list. Let’s implement a queue using a Python list.

#creating a queue using list

#defining a list

my_queue =[]

#inserting the items in the queue

my_queue.append(1)

my_queue.append(2)

my_queue.append(3)

my_queue.append(4)

my_queue.append(5)

print("The items in queue:")

print(my_queue)

#removing items from queue

print(my_queue.pop(0))

print(my_queue.pop(0))

print(my_queue.pop(0))

print(my_queue.pop(0))

#printing the queue after removing the elements

print("The items in queue:")

print(my_queue)

In the above given example, we have passed index 0 to the pop() function because we have to remove the first added item.

Output

As discussed previously, we can store any type of items in the list, so let’s create a queue using a list that contains the items of heterogeneous type.

#creating a queue using list

#defining a list

my_queue =[]

#inserting the items in the queue

my_queue.append(1)

my_queue.append("a")

my_queue.append(2)

my_queue.append("b")

my_queue.append(3)

print("The items in queue:")

print(my_queue)

#removing items from queue

print(my_queue.pop(0))

print(my_queue.pop(0))

print(my_queue.pop(0))

print(my_queue.pop(0))

#printing the queue after removing the elements

print("The items in queue:")

print(my_queue)

Output

Queue implementation using the queue.Queue

The queue is a Python built-in module that is used to create and implement the queues. The max size attribute initializes the size of the queue. Many functions are available in the queue module. For instance, the put() function adds the item to the queue, the get() function removes the element from the queue, and the qsize() function returns the queue size. Let’s implement a queue using the queue module.

#importing the queue module

import queue

#creating a queue of size 5

my_queue = queue.Queue(maxsize=5)

#inserting the items in the queue

my_queue.put(1)

my_queue.put(2)

my_queue.put(3)

my_queue.put(4)

my_queue.put(5)

print("The size of queue is:",my_queue.qsize())

#removing items from queue

print(my_queue.get())

print(my_queue.get())

print(my_queue.get())

print(my_queue.get())

print("The size of queue is:",my_queue.qsize())

Output

Queue implementation using the deque

The deque is a class of collections module. As compared to the list, the deque performs the enqueue and dequeue operations quicker. The append() and popleft() functions insert and remove the items from the queue, respectively. Let’s implement a queue using deque.

#importing the collections module

import collections

#creating a queue

my_queue = collections.deque()

#inserting the items in the queue

my_queue.append(1)

my_queue.append(2)

my_queue.append(3)

my_queue.append(4)

my_queue.append(5)

print("The items in the queue are:")

print(my_queue)

#removing items from queue

print(my_queue.popleft())

print(my_queue.popleft())

print(my_queue.popleft())

print(my_queue.popleft())

print("The items in the queue are:")

print(my_queue)

Output

Conclusion

A queue is used to store and manage the data. It stores the data in FIFO order. The queue in Python can be implemented in three ways, which are explained in this article with examples.



from Linux Hint https://ift.tt/33vZ7Xy

Post a Comment

0 Comments