Clicky

Basic Python Programming Code Snippets: Queues


Basic Python Programming Code Snippets:

Queue Data Structure

March 28, 2023

Basic Python

Queues are an essential data structure in computer science that follow the First-In-First-Out (FIFO) principle, making them ideal for managing data in a specific order. Python, a popular high-level programming language, provides built-in support for queues, allowing programmers to easily implement this data structure in their code. In this article, we will delve into the basics of Python queues and ways in which they are commonly implemented. Whether you are a beginner or an experienced programmer, this article will equip you with the knowledge and skills to leverage the power of Python queues in your code.

Basic Python Queues

Methods that Enforce FIFO of Queues

To add elements to a queue, the append() and put() methods are used (depending on the approach used to implement the queue). To remove elements from a queue, the pop(), popleft(), and get() method are used (depending on the approach used to implement the queue). These methods enforce the FIFO rule of queues. All three (3) approaches are described below.


To add an element to the end of the queue, use the following syntax:

queue.append(x)     #Add an item to the end of the queue.  Methods #1 and #2

queue.put(x)     #Add an item to the end of the queue.  Method #3

To remove an element from the end of the queue, use the following syntax:

queue.pop()     #removes and returns the first item in the queue.  Method #1

queue.popleft()     #removes and returns the first item in the queue.  Method #2

queue.get()     #removes and returns the first item in the queue.  Method #3


Method #1: Implement Queue Using a List


Create a Queue Using a List

Using a Python list, we can create a queue.

queue = [] # create an empty queue

Add Elements to a Queue Implemented as a List

queue.append(1) # enqueue 1 to the queue
queue.append(2) # enqueue 2 to the queue

Remove Elements from a Queue Implemented as a List

queue.pop(0) # dequeue and return the first item in the queue


Method #2: Implement Queue Using collections.deque


Create a Queue Using collections.deque

Using collections.deque, the deque class from the collections module, we can create a queue.

from collections import deque
queue = deque() # create an empty queue

Add Elements to a Queue Using collections.deque

queue.append(1) # enqueue 1 to the queue
queue.append(2) # enqueue 2 to the queue

Remove Elements from a Queue Using collections.deque

queue.popleft() # dequeue and return the first item in the queue


Method #3: Implement Queue Using queue.Queue


Create a Queue Using queue.Queue

Using queue.Queue, the Queue class from the queue module, we can create a queue.

from queue import Queue
queue = Queue() # create an empty queue

Add Elements to a Queue Using queue.Queue

queue.put(1) # enqueue 1 to the queue
queue.put(2) # enqueue 2 to the queue

Remove Elements from a Queue Using queue.Queue

queue.get() # dequeue and return the first item in the queue


Video Demonstration of Queue Behavior




Conclusion

Python queues are a powerful tool for managing data in a specific order, making them essential in a wide range of programming applications. Whether you are working on a simple project or a complex system, understanding how to use queues can greatly improve your code's efficiency and performance.


Related Articles

For more articles on Python data structures, see the following:



Learn to Code with Educative.io

Visit Educative.io

Educative

Tutorials Point Offers Python Programming Courses

Polish Your Python Skills. Visit Tutorials Point!

Tutorials Point


Integrate Data Science into Your Learning Experience with DataCamp

Visit DataCamp Today!