Enqueue in Queues in Python Last Updated : 16 Apr, 2024 Comments Improve Suggest changes Like Article Like Report A queue is a basic data structure that follows the First-In-First-Out (FIFO) principle. At the back of the queue, elements are added (enqueued), and at the front, they are removed (dequeued). In this article, we will see the methods of Enqueuing (adding elements) in Python. Enqueue: The act of adding an element to the rear (back) of the queue.Dequeue: The act of removing an element from the front of the queue.FIFO (First-In-First-Out): Elements are processed in the order they are added. The first element added will be the first element removed.Know More about Queues in Python Click Here Enqueue in Queues in PythonBelow, are the methods of Enqueue in Queues in Python. Using collections.deque()Using Custom Queue ClassEnqueue in Queues Using collections.dequeIn this example, below Python code uses the deque module to create a queue. Elements like "apple", "banana", and "cherry" are enqueued into the queue using the append() method. Finally, the content of the queue is printed, showing the order of elements added to it. Python3 from collections import deque # Create a queue queue = deque() # Enqueue elements queue.append("apple") queue.append("banana") queue.append("cherry") print(queue) Outputdeque(['apple', 'banana', 'cherry']) Enqueue in Queues Using Custom Queue ClassIn this example, below code defines a Queue class with methods for enqueueing items. Instances maintain a list of items internally. Items are enqueued using the enqueue() method, but directly accessing the list for queue functionality isn't recommended. Python3 class Queue: def __init__(self): self.items = [] def enqueue(self, item): self.items.append(item) # Create a queue queue = Queue() # Enqueue elements queue.enqueue("orange") queue.enqueue("mango") queue.enqueue("grapefruit") # Accessing elements directly from the list is not recommended # for queue functionality (use dequeue method instead) print(queue.items) Output['orange', 'mango', 'grapefruit'] Comment More infoAdvertise with us Next Article Enqueue in Queues in Python H heysaiyad Follow Improve Article Tags : Queue Python Python Programs Python-DSA Practice Tags : pythonQueue Similar Reads Creating Queue Using Collections.Deque In Python In Python, Queue and Deque are the data structures used for managing collections of elements in a first-in, first-out (FIFO) manner. In this article, we will learn to implement queues using collections.deque in Python. Create Queue Using Collections.deque In PythonBelow, is the Implementation of Cre 3 min read Implementation of Deque Using List in Python A Deque (double-ended queue) is a data structure that allows insertion and deletion from both the front and rear. Deques are widely used in applications requiring efficient access and modification at both ends. While Python provides a built-in collections.deque for optimized performance, a deque can 5 min read How to Use Lists as Stacks and Queues in Python In Python, lists can be used to implement a variety of data structures, including stacks and queues. In this article, weâll explore how to use Python lists to create and perform basic operations on stacks and queues.Using List as Stack in PythonA stack is a fundamental data structure that follows th 4 min read How to Parallelize a While loop in Python? Parallelizing a while loop in Python involves distributing the iterations of a loop across multiple processing units such as the CPU cores or computing nodes to execute them concurrently. This can significantly reduce the overall execution time of the loop, especially for tasks that are CPU-bound or 2 min read Differences between Python Parallel Threads and Processes In Python, parallelism is a powerful concept used to execute multiple tasks concurrently by improving performance and efficiency. The Two common approaches to parallelism in Python are parallel threads and parallel processes. While both achieve concurrent execution they have distinct characteristics 3 min read Like