View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Queue in Python: Implementation Using List, deque, and Queue Module

Updated on 28/05/20255,795 Views

When it comes to data structures, the queue in Python plays a pivotal role in handling data in a linear way. Just like the queue at a coffee shop, first come, first served, a queue in Python works on the FIFO (First In First Out) principle. That means the first element added is the first one to be removed.

In many software engineering & development courses, queues are introduced early on because of their fundamental importance in areas like task scheduling, asynchronous programming, and real-time data processing. Mastering the queue in Python helps in building scalable applications and solving real-world problems efficiently.

In this blog, we'll explore everything about the queue in Python, from basic operations to advanced implementations using built-in modules. Whether you're a beginner learning the ropes or a developer looking to refine your skills, understanding how to work with a queue in Python is essential for writing efficient and clean code.

Operations Associated with Queue in Python

Before diving into implementation, it’s crucial to understand the fundamental operations that define how a queue in Python works. These core actions mimic real-life queues and are essential for managing data flow.

Here are the primary operations associated with a queue in Python:

  • Enqueue (Insertion): This operation adds an element to the rear (or end) of the queue. In the context of the queue in Python, this can be done using `append()` or methods provided by specialized libraries.
  • Dequeue (Removal): Dequeue removes an element from the front of the queue. This operation ensures the FIFO behavior is maintained. Attempting to dequeue from an empty queue in Python usually raises an error or returns a special value, depending on the method used.
  • Peek / Front: Peek allows you to look at the front element of the queue without removing it. This is useful when you need to check the next item to be processed in a queue in Python.
  • IsEmpty: This operation checks whether the queue is empty. It’s especially helpful to prevent errors during dequeue operations when working with a queue in Python.
  • Size: Returns the number of elements in the queue. Knowing the size of a queue in Python helps in performance monitoring and resource management.

Pilot your high-paying career with the following full-stack development courses: 

Methods Available for Queue in Python

Python provides multiple built-in ways to implement and work with queues, each offering a set of methods tailored to specific use cases. Depending on the complexity and concurrency needs of your application, you can choose from several queue-related tools. Let’s explore the most commonly used methods for managing a queue in Python.

Read the String Split in Python article to develop efficient Python projects.

1. List Methods

When using a Python list as a queue in Python, these methods come in handy: 

  • append(item): Adds an element to the end of the list (enqueue).
  • pop(0): Removes the first element of the list (dequeue).

However, lists are not optimal for large-scale queue operations because popping from the front is O(n) in time complexity.

2. collections.deque Methods

This is a preferred method for implementing a queue in Python efficiently.

  • append(item): Adds to the rear of the queue.
  • popleft(): Removes from the front with O(1) time complexity.
  • extend(iterable): Adds multiple items to the end.
  • clear(): Removes all elements.
  • rotate(n): Rotates the queue n steps to the right.

Must explore the Python Frameworks article to master modern web frameworks

3. queue.Queue Methods

Ideal for multi-threaded programming, this class is built to be thread-safe and used in concurrent scenarios.

  • put(item): Enqueues an item safely.
  • get(): Dequeues an item safely.
  • qsize(): Returns the approximate number of items.
  • empty(): Checks if the queue is empty.
  • full(): Checks if the queue has reached its max size.

Using the right method can dramatically improve your application's performance and reliability. When choosing how to implement a queue in Python, consider the expected data volume and whether multithreading is involved.

Implement a Queue in Python

Now that we've explored the operations and methods, let’s dive into the fun part—actually implementing a queue in Python. Whether you're preparing for interviews or building real-world applications, understanding how to build a queue in Python from scratch or using libraries is crucial.

There are three main ways to implement a queue in Python, each with its own use cases:

  • Using a List 
  • Utilizing collections.deque 
  • Using queue.Queue

In the sections ahead, we’ll walk through each of these implementations step-by-step. Every code snippet will show how to add, remove, and view elements in a queue in Python, with clear explanations and output examples.

Go through the Comments in Python article to write cleaner, modular code.

Implementing a Queue in Python with a List

Using a list is the most straightforward way to implement a queue in Python, especially for beginners or when working with small datasets. However, it comes with a performance caveat: removing elements from the front of a list is an O(n) operation, which can slow things down for large queues.

Here’s a simple way to implement a queue in Python using lists.

Code Example: Queue with List

# Creating a queue using a list in Python

# Initialize an empty list
queue = []

# Enqueue operation – add elements to the end
queue.append('apple')
queue.append('banana')
queue.append('cherry')

# Print current queue
print("Queue after enqueue operations:", queue)

# Dequeue operation – remove element from the front
removed_item = queue.pop(0)

# Print updated queue
print("Item removed (dequeue):", removed_item)
print("Queue after dequeue:", queue)

Output

Queue after enqueue operations: ['apple', 'banana', 'cherry']

Item removed (dequeue): apple

Queue after dequeue: ['banana', 'cherry']

Explanation

  • We use append() to add items to the end of the queue, mimicking the enqueue operation.
  • pop(0) removes the item from the front, simulating a dequeue.

This approach is easy to understand, making it a good starting point to grasp the behavior of a queue in Python.

However, as your data size grows, the performance hit from `pop(0)` becomes noticeable. That’s where the `collections.deque` module comes in handy, which we’ll explore next.

Read the Merge Sort in Python article to boost your programming skills.

Implementing a Queue in Python with collections.deque

If you're looking for an efficient way to implement a queue in Python, `collections.deque` is your go-to option. Deque (short for "double-ended queue") provides fast appends and pops from both ends—making it ideal for FIFO-based operations.

This method is highly optimized and recommended for most queue use cases in Python.

Code Example: Queue with collections.deque

from collections import deque

# Creating a queue using deque
queue = deque()

# Enqueue operation – add elements to the rear
queue.append('dog')
queue.append('cat')
queue.append('rabbit')

# Print current queue
print("Queue after enqueue operations:", queue)

# Dequeue operation – remove element from the front
removed_item = queue.popleft()

# Print updated queue
print("Item removed (dequeue):", removed_item)
print("Queue after dequeue:", queue)

Output

Queue after enqueue operations: deque(['dog', 'cat', 'rabbit'])

Item removed (dequeue): dog

Queue after dequeue: deque(['cat', 'rabbit'])

Explanation

  • We use append() to add elements to the queue (rear end).
  • popleft() is used to remove the element from the front, preserving the FIFO behavior of a queue in Python.
  • Unlike lists, both operations run in constant time (O(1)), making `deque` a highly efficient structure for queue implementations.

This method is suitable for most day-to-day applications of a queue in Python, and it doesn’t require any additional thread-safety features.

Read the Inheritance in Python article to efficiently implement an important OOPS concept.

Implementing a Queue in Python with queue.Queue

For applications involving multithreading or concurrent tasks, thread safety becomes critical. The `queue.Queue` module in Python is designed exactly for this purpose. It allows safe and efficient sharing of data between multiple threads using built-in locking mechanisms.

This implementation is perfect when building producer-consumer systems, task schedulers, or managing asynchronous workflows in Python applications.

Code Example: Queue with queue.Queue

import queue

# Create a FIFO queue with no size limit
q = queue.Queue()

# Enqueue operation – safely add elements to the queue
q.put('task1')
q.put('task2')
q.put('task3')

# Print queue size
print("Queue size after enqueue:", q.qsize())

# Dequeue operation – safely remove an element from the front
removed_item = q.get()

# Print removed item and new size
print("Item removed (dequeue):", removed_item)
print("Queue size after dequeue:", q.qsize())

Output

Queue size after enqueue: 3

Item removed (dequeue): task1

Queue size after dequeue: 2

Explanation

  • put(): Adds an item to the queue in a thread-safe manner.
  • get(): Removes and returns the front item safely.
  • qsize(): Returns the number of elements currently in the queue.

This version of queue in Python is ideal for programs that require coordination between threads, preventing race conditions and ensuring data integrity.

If your project involves concurrency or parallelism, `queue.Queue` should be your preferred option for implementing a queue in Python. 

Read the Operators in Python article to build scalable web applications.

Examples of Queue in Python 

Now that we've explored multiple ways to implement a queue in Python, let’s take a practical approach. Here are several examples that demonstrate how queues are used in real-world Python applications—from simple operations to concurrent processing.

These examples will help you solidify your understanding of a queue in Python while showing how flexible and powerful it can be in different contexts.

Must explore the OpenCV in Python article to enhance your coding productivity.

1. Add and Remove Elements in a Queue

We’ll use `collections.deque` for efficient element management.

from collections import deque

# Create a queue
queue = deque()

# Enqueue elements
queue.append('A')
queue.append('B')
queue.append('C')

# Dequeue an element
removed = queue.popleft()

print("Removed:", removed)
print("Current Queue:", queue)

Output:

Removed: A

Current Queue: deque(['B', 'C'])

Explanation:

We added three elements and removed one from the front, preserving FIFO behavior.

2. Sort a Queue (Convert to List, Sort, and Convert Back)

While queues aren’t naturally sorted structures, you can sort the data if needed.

from collections import deque

# Unsorted queue
queue = deque([3, 1, 4, 2])

# Sort by converting to list
sorted_queue = deque(sorted(list(queue)))

print("Sorted Queue:", sorted_queue)

Output:

Sorted Queue: deque([1, 2, 3, 4])

Explanation:

By converting the queue in Python to a list, sorting it, and converting it back, you temporarily override FIFO for specific tasks like priority handling.

Read the Memory Management in Python article to speed up development time.

3. Multiprocessing with queue.Queue

Here's how a queue in Python can be used for multiprocessing with `multiprocessing.Queue`.

from multiprocessing import Process, Queue

# Function to add items to the queue
def producer(q):
    q.put("Hello from producer")

# Function to get items from the queue
def consumer(q):
    msg = q.get()
    print("Received:", msg)

if __name__ == '__main__':
    q = Queue()

    # Create producer and consumer processes
    p1 = Process(target=producer, args=(q,))
    p2 = Process(target=consumer, args=(q,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

Output:

Received: Hello from producer

Explanation:

Using `multiprocessing.Queue` allows safe inter-process communication. This is crucial when building concurrent apps or pipelines where a queue in Python manages data between processes.

Each of these examples showcases how flexible a queue in Python can be. From simple data storage to multi-threaded or multi-process communication, queues are foundational tools in Python programming and software development.

Read Reverse String in Python article to understand core string concept. 

Conclusion 

The queue in Python is an essential data structure that allows developers to manage data efficiently using the First In, First Out (FIFO) principle. Whether you choose to implement a queue using simple lists, the highly optimized collections.deque, or the thread-safe queue.Queue for concurrent applications, mastering these options will greatly enhance your ability to build responsive and organized programs. 

As you continue your programming journey, especially in areas like task scheduling, asynchronous programming, and multiprocessing, the queue in Python will prove invaluable. With a solid grasp of queues and related data structures, you’ll be well-equipped to write clean, efficient, and scalable code that can handle real-world challenges with ease.

FAQs

1. What is a queue in Python and how does it work?

A queue in Python is a linear data structure following the FIFO (First In, First Out) principle. Elements are added at the rear and removed from the front, ensuring that data or tasks are processed in the exact order they arrive, maintaining proper sequencing.

2. How is a queue different from a stack in Python?

A queue operates on FIFO, meaning the first element added is the first to be removed. A stack, on the other hand, follows LIFO (Last In, First Out), where the last added element is removed first. Each is suited for different problem-solving scenarios.

3. What are the common operations performed on a queue in Python?

Common operations in a queue include enqueue (adding elements at the rear), dequeue (removing from the front), peek (viewing the front item without removal), checking if the queue is empty, and getting the size of the queue for management purposes.

4. Why is collections.deque preferred over a list for implementing a queue?

The collections.deque provides O(1) time complexity for appending and popping elements from both ends, whereas a list has O(n) complexity for removing elements at the front. This makes deque far more efficient and suitable for implementing queues in Python.

5. When should I use queue.Queue instead of collections.deque in Python?

Use queue.Queue when working with multi-threaded programs requiring thread-safe queue operations. It provides internal locking mechanisms preventing race conditions, while collections.deque is not thread-safe and better suited for single-threaded applications or simple queue use cases.

6. Can I sort elements inside a queue in Python?

Queues generally do not support sorting as they enforce FIFO order. However, you can convert the queue to a list, perform sorting, and then rebuild the queue if temporary sorted access is needed. This approach is useful in certain priority or scheduling tasks.

7. How does multiprocessing.Queue differ from queue.Queue in Python?

Multiprocessing.Queue is designed to facilitate communication between different processes with data safely shared across them, while queue.Queue is intended for thread-safe communication within a single process among multiple threads, each handling concurrency differently.

8. Is it possible to implement a priority queue using the queue module?

The standard queue module does not support priority queues. However, Python provides queue.PriorityQueue, which allows elements to be dequeued based on priority order rather than arrival order, and is also thread-safe, useful in complex scheduling scenarios.

9. What happens if I try to dequeue from an empty queue in Python?

Behavior depends on the queue implementation. queue.Queue’s get() method blocks until an item is available, preventing errors, whereas popping from an empty list or deque raises an IndexError. Always check if the queue is empty before dequeueing.

10. Are queues useful in real-world Python applications?

Yes, queues are widely used in real-world applications like task scheduling, breadth-first search algorithms, managing requests in web servers, asynchronous workflows, and multiprocessing. They ensure data or tasks are handled sequentially and efficiently across various domains.

11. How do I check if a queue is empty in Python?

To check if a queue is empty, use the `empty()` method for queue.Queue. For lists or collections.deque, checking if `len(queue) == 0` indicates emptiness. This check helps avoid errors when performing dequeue operations or processing data.

image

Take our Free Quiz on Python

Answer quick questions and assess your Python knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.