avatarFahadul Shadhin

Summary

The article discusses the implementation of efficient queues in Python using the deque data structure from the collections module.

Abstract

The collections module in Python provides specialized container data types, including deque, which stands for double-ended queue. This article explains how deque can be used to create a custom queue that allows for efficient append and pop operations at both ends of the queue. The author illustrates the basics of deque, including how to import and initialize it, and demonstrates how to perform pop() and append() operations. A custom Queue class is then developed, incorporating methods for enqueuing and dequeuing elements, retrieving the front and rear elements, and additional functionalities such as length retrieval, iteration, membership check, and a string representation. The article concludes by emphasizing the efficiency of deque-based queues compared to lists due to their doubly-linked-list implementation, which offers faster and more memory-efficient operations on either side of the sequence.

Opinions

  • The author suggests that using deque for implementing queues is more efficient than using lists.
  • It is implied that understanding how to implement a queue with a list is beneficial before learning the deque approach.
  • The article promotes the collections module as a source of specialized container data types beyond the built-in list, set, tuple, and dictionary.
  • The author provides a user-friendly Queue class with comprehensive functionalities, indicating a focus on practical application and ease of use for developers.
  • The article is positioned as a follow-up to a previous discussion on queues, indicating an educational series or progression in learning data structures.
  • By providing references to external resources, the author encourages further reading and learning on the topic of deque and its applications.

Implement Efficient Queues Using Python’s collections Module

Create custom queues using deque in Python’s collections module

Photo by Emile Guillemot on Unsplash

The collections module in Python implements some specialized container data types in addition to built-in containers, dictionary, list, set, and tuple. deque is one of them. Using deque we can create our custom queue data structure efficiently.

In one of my previous articles, I discussed the simplest way to implement a queue in Python using lists. To grasp a proper understanding of how a queue works you can read this article.

Today I will discuss a more efficient approach to creating a custom queue using Python’s collections module. This module provides us with the following container data types:

Containers in collections module | Screenshot taken from Python documentation

deque will help us to implement efficient and fast queues.

The Basics of deque

To use deque we need to import it from the collections module:

from collections import deque

Creating a deque is fairly straightforward:

Output:

deque([])
deque([1, 2, 3, 4, 5])
deque(['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'])
deque([('one', 1), ('two', 2), ('three', 3)])

deque stands for double-ended queue. That means we can perform pop() and append() operations on both ends of the sequence. Let’s see how we can perform pop() and append() operations in a deque:

Output:

1
2
deque([10, 20, 3, 4, 5])
5
4
deque([10, 20, 3, 100, 200])

This knowledge should be enough to implement our custom queue.

Creating a Custom Queue Class Using deque

A simple queue may have the following operations:

  • enQueue(). Adding items to the queue
  • deQueue(). Removing items from the queue
  • Retrieving the front element of the queue
  • Retrieving the rear element of the queue

We can modify append(), and popleft() to implement enQueue(), and deQueue() operations in a queue respectively.

We can add the following additional functionalities to the Queue class:

  • Returning the length of the queue
  • Supporting membership check
  • Supporting iterations on the queue
  • Providing a user-friendly string representation of the queue

Here is the complete Queue class:

Let’s summarize the features of the Queue class:

  • enQueue() will add elements to the queue
  • deQueue() will remove elements from the queue
  • frontElement() will return the front element of the queue
  • rearElement() will return the rear element of the queue
  • __len__() will return the length of the queue
  • __iter__() will allow iterations on the queue
  • __reveresed__() will allow backward iterations
  • __contains__() will allow membership check in the queue
  • __repr__() will provide a user-friendly string representation of the queue

Using the Queue Class

We can use the Queue class like the following:

Output:

Queue([])
Queue([1, 2, 3, 4, 5])
Queue([3, 4, 5])
3
False
True
3
4
5

Conclusion

deque is implemented based on doubly-linked-list. It is optimized for operations on either side of a sequence. On the other hand, list is implemented based on array. Pop and append operations are faster, and more memory-efficient in deque than list. So implementing queues using deque is more efficient.

That’s it for today. I hope you find this article helpful. Thanks for reading.

References

Programming
Python
Data Structures
Data Science
Coding
Recommended from ReadMedium