Deque : Memory Efficient Alternative To Python Lists
- Naveen
- 0
In this blog, we will be covering deque, which stands for Double Ended Queue in Python. We will explore why this data structure is very useful, especially when managing a stack in Python. We will go over the methods that come with the Double Ended Queue and how we can use it to handle queues in any kind of system.
To begin, let’s import the deque
module from the collections
library. Using the Double Ended Queue is quite simple. First, let’s create a variable called people
and assign it an array of characters, such as Mario, Luigi, and Toad.
from collections import deque people = ['Mario', 'Luigi', 'Toad']
Next, we can create a queue by calling deque
and passing in our list. This will convert the list into a Double Ended Queue.
queue = deque(people)
The deque
object can be treated as a normal list. For example, if we want to access the element at index 1, we can simply use indexing:
print(queue[1]) # Output: Luigi
One of the most common methods in a queue is the append
method, which adds an element to the end of the queue. Let’s say Bowser wants to join the queue. We can use the append
method to add Bowser to the queue.
queue.append('Bowser') print(queue) # Output: deque(['Mario', 'Luigi', 'Toad', 'Bowser'])
If we want to remove an element from the queue, we can use the pop_left
method. This method removes and returns the leftmost element in the queue. Let’s say Mario has left the queue. We can use the pop_left
method to remove Mario.
queue.pop_left() print(queue) # Output: deque(['Luigi', 'Toad', 'Bowser'])
Now, let’s imagine that Daisy decided to cut the line and join the queue at the front. We can use the append_left
method to add Daisy to the front of the queue.
queue.append_left('Daisy') print(queue) # Output: deque(['Daisy', 'Luigi', 'Toad', 'Bowser'])
If Daisy decides to go on the ride and then join the back of the queue again, we can create a rotation using the rotate
method. By passing in the index of -1, we can move everyone to the left, effectively rotating the queue.
queue.rotate(-1) print(queue) # Output: deque(['Luigi', 'Toad', 'Bowser', 'Daisy'])
The rotate
method allows us to create different rotations by passing in different indices. For example, if two people went on the ride, we can rotate the queue by -2.
queue.rotate(-2) print(queue) # Output: deque(['Bowser', 'Daisy', 'Luigi', 'Toad'])
If we want to append multiple people to the end of the queue, we can use the extend
method. This method takes an array as an argument and adds each element to the end of the queue.
queue.extend(['Shy Guy', 'Yoshi']) print(queue) # Output: deque(['Bowser', 'Daisy', 'Luigi', 'Toad', 'Shy Guy', 'Yoshi'])
Alternatively, we can use the extend_left
method to add elements to the front of the queue.
queue.extend_left(['Shy Guy', 'Yoshi']) print(queue) # Output: deque(['Yoshi', 'Shy Guy', 'Bowser', 'Daisy', 'Luigi', 'Toad', 'Shy Guy', 'Yoshi'])
Lastly, the reverse
method can be used to reverse the order of elements in the queue. This method is useful when we want to change the order of the queue.
queue.reverse() print(queue) # Output: deque(['Yoshi', 'Shy Guy', 'Toad', 'Luigi', 'Daisy', 'Bowser'])
At the end of the day, it is important to use what you find most convenient. Using the deque
instead of a normal list can make more sense when working with queues, as it provides additional functionality without requiring unnecessary computations.
Conclusion
We covered the Double Ended Queue in Python, also known as deque
. We explored the various methods that come with the deque
object and how they can be used to manage queues in any system. The deque
object provides convenient methods such as append
, pop_left
, append_left
, rotate
, extend
, extend_left
, and reverse
. By using the deque
object, we can handle queues efficiently and effectively.
If you found this article helpful and insightful, I would greatly appreciate your support. You can show your appreciation by clicking on the button below. Thank you for taking the time to read this article.
Popular Posts
- From Zero to Hero: The Ultimate PyTorch Tutorial for Machine Learning Enthusiasts
- Day 3: Deep Learning vs. Machine Learning: Key Differences Explained
- Retrieving Dictionary Keys and Values in Python
- Day 2: 14 Types of Neural Networks and their Applications