- How to check if a deque is empty?
- What is a queue?
- simple queue
- circular queue
- priority queue
- double-ended queue
- Why do we need deque?
- How to implement a deque?
- Python Implementation of Deque
- Functions for deque
- Append function
- Appendleft function
- Pop function
- Popleft function
- Can a deque be empty?
- Creating an empty deque in Python
- How to check if a deque is empty?
- Python code to check if a deque is empty
- Why do we need to check if a deque is empty?
- Conclusion
- Python – Check If a Deque is Empty
- deque in Python
- How to check if a deque is empty?
- Examples
- Example 1 – Check if a deque is empty in Python
- Example 2 – Check if a bounded deque is empty
- Author
How to check if a deque is empty?
Deque or double-ended queue is an extension of the queue that allows insertion and deletion from both ends of the queue. In this article, we’re going to learn about queues, different types of queues, deque in-depth, and finally, how to check if a deque is empty. Let’s get started!
What is a queue?
A queue is a linear data structure that follows the FIFO rule. The FIFO rule is also known as the first in first out rule. In a queue, data enters from the front and exits from the back. It has a ton of applications in file IO, sockets, etc.
Related: Learn about the queue in Python in depth.
There are mainly 4 types of queues:
- simple queue
- circular queue
- priority queue (heapq)
- double ended queue
simple queue
A simple queue is a classic queue, which follows the FIFO rule. In this type of queue, we track the element at the front and the back of the queue. The simple queue has a lot of limitations, it being the most basic form of a queue.
One of the limitations is that if the queue is full and we delete an element from the queue then there will be some empty space available at the front of the queue but we won’t be able to insert new elements. This leads to a waste of memory. To avoid this problem we use a circular queue.
circular queue
In a circular queue, when the queue is full it starts appending elements at the front. A circular queue leads to no memory wastage as it utilizes all the space allocated for the queue. It only shows the queue is full when there is genuinely no space left in the queue, neither at the back nor at the front.
The insertion formula for the circular queue is (rear+1)%size where the rear is the index of the last element and size is the size of the queue.
priority queue
The priority queue is a queue in which the elements are stored in the queue as per their priority. In the priority queue, the element with the highest priority will be popped first. It can be implemented using a heap. Heap is a complete binary tree in which the leaf nodes have low priority whereas the root node has the highest priority. There are two types of – min heap and max heap.
double-ended queue
In a simple queue, we can append and pop only on one end. This restriction is what makes a queue a queue. But sometimes we need to append or pop from the same ends or even both ends. In those cases, a double-ended queue also known as deque can be used. Deque is a linear data structure that allows append and pop from both ends in O(1) time.
Related: Learn double-ended queues in depth.
Why do we need deque?
Deque allows insertion and deletion from both ends. It has easy implementation but leads to a pool of possibilities. It can be used as a queue or stack at the same time. The ability to perform deletion from both ends in O(1) time makes it easier and faster to perform a palindrome check. It also has applications in memory management and some graph-based problems. Deque opens the gate for a lot of applications.
How to implement a deque?
As we know, Python is enriched with modules and libraries. Python has a module named collections which provides us with the deque data structure. Using the deque from collections we can implement deque easily.
The deque class takes a list as the parameter and converts the given list into a deque.
Python Implementation of Deque
from collections import deque #importing deque dq = deque([1,2,3,4,5]) #using deque to create a deque of a list print(dq)
Output
deque([1, 2, 3, 4, 5])
Functions for deque
Append function
The append function appends the given element at the end of the deque.
Syntax :- deque.append(6)
Output :- deque([1,2,3,4,5,6])
Appendleft function
The append function appends the given element at the start of the deque.
Syntax :- deque.appendleft(0)
Output :- deque([0,1,2,3,4,5,6])
Pop function
The pop function removes the last element of the deque.
Syntax:- deque.pop()
Output:- deque([1,2,3,4,5])
Popleft function
The pop function removes the first element of the deque.
Syntax:- deque.popleft()
Output:- deque([2,3,4,5])
Can a deque be empty?
It is possible for a deque to be empty. A deque is said to be empty if there is no element present in the deque.
There are two ways in which a deque can be empty:
- We pass an empty list as an argument of the deque.
- We pop all the elements of the deque using the pop or popleft functions of the deque.
Creating an empty deque in Python
from collections import deque #importing deque dq = deque([]) #using deque to create a deque of a list print(dq)
OUTPUT :- deque([])
In the above code, we just passed an empty list as the argument of the deque. This way we’ll have an empty deque.
How to check if a deque is empty?
We learned how to create an empty deque. But what if we have a deque and we have to check if it is empty or not? An empty deque refers to a deque with no elements in it. If a deque has no elements in it then as a consequence it will have a length of 0. So to check if a deque is empty, we’ll have to check its length. If the length of the deque is 0, it’s an empty deque.
Let’s see how we can do this in code.
Python code to check if a deque is empty
from collections import deque #importing deque dq = deque([]) #using deque to create a deque of a list #creating an if-else block to check the length of the deque and output the result if len(dq)==0: print("The deque is empty!") else: print(f"There is/are elements in the deque.")
In the above code, first, we imported a deque from collections and then created a deque with an empty list as an argument. Then we wrote an if-else structure to check if the deque is empty. We set the condition of the if statement as len(dq)==0. The “len” function gives the length of the deque as output. So it basically means, if dq has a length of 0, output empty deque else output the no. of elements in the deque.
from collections import deque #importing deque dq = deque([1,2,3]) #using deque to create a deque of a list #creating an if-else block to check the length of the deque and output the result if len(dq)==0: print("The deque is empty!") else: print(f"There is/are elements in the deque.")
This time we passed the list [1,2,3] as the argument. This list has 3 elements in it. So len(dq)==0 will be false and henceforth the interpreter will execute the else block. So the output will be “There is/are 3 elements in the deque.”
Why do we need to check if a deque is empty?
Deque is commonly used in graph questions or dfs/bfs. While working with them, we constantly need to check the status of the deque. So it is necessary to learn to check if a deque is empty.
Conclusion
Deque is an important data structure that has a lot of applications. It is very simple to implement and has great functionalities. It is faster and way more flexible than a simple queue or a stack. Make sure you understand it completely and learn how to apply it. The deque has its own drawbacks but also has a ton of advantages. Keep exploring and be open to learning new things. Make sure you also check how to use other types of queues like circular queues and priority queues.
Python – Check If a Deque is Empty
In this tutorial, we will look at how to check whether a deque in Python is empty or not with the help of some examples.
deque in Python
The collections module in Python comes with a deque class that you can use to implement a stack or queue data structure in Python. Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
In simple terms, you can think of a deque in Python as a list-like data structure but with efficient insertion and removal of items from either end (left and right). This makes them very useful when implementing a stack or queue functionality in Python.
How to check if a deque is empty?
Deques in Python are collection-type objects. Similar to other collection types in Python such as lists, tuples, etc. you can check whether a deque object is empty or not by using it in a boolean context.
The following is the syntax –
# check if deque "queue" is empty not queue
Using a deque object in a boolean context will give True if the deque is not empty and False if the deque is empty. Since we directly want to check whether the deque is empty or not, we use
not queue .
Alternatively, you can check if a deque is empty or not by checking if its length is equal to 0.
The following is the syntax –
# check if deque "queue" is empty len(queue)==0:
Examples
Let’s now look at some examples of using the above syntax
Example 1 – Check if a deque is empty in Python
First, let’s create two deques, one empty and the other non-empty (containing some elements).
from collections import deque # create an empty deque q1 = deque() # create a non-empty deque q2 = deque([1, 2, 3]) # print the deques print(q1) print(q2)
Here, we imported the deque class from the collections module and created two deque objects – q1 , an empty deque, and q2 a deque with some values.
Let’s now check if the above-created deque objects are empty or not by using them in a boolean context.
# check if deque is empty print(not q1) print(not q2)
We get True for q1 (indicating that it’s empty) and False for q2 (indicating that it’s not empty).
You can also check if a deque is empty or not by comparing its length to 0.
# check if deque is empty print(len(q1)==0) print(len(q2)==0)
We get the same result as above.
Example 2 – Check if a bounded deque is empty
You can use the above methods to check if a deque is empty or not for bounded deques (deques with a specified maximum size) as well.
Let’s create two bounded deques, one empty and the other non-empty.
# create an empty bounded deque q1 = deque(maxlen=4) # create a non-empty deque q2 = deque([1, 2, 3], maxlen=4) # print the deques print(q1) print(q2)
deque([], maxlen=4) deque([1, 2, 3], maxlen=4)
Here, we created two bounded deques – q1 , an empty deque, and q2 a deque with some values.
Let’s now use check if these deques are empty or not by using them in a boolean context.
# check if deque is empty print(not q1) print(not q2)
We get True for q1 (indicating that it’s empty) and False for q2 (indicating that it’s not empty).
We can also similarly use the len() function to get their respective lengths and compare them with 0 to check whether the deques are empty or not.
# check if deque is empty print(len(q1)==0) print(len(q2)==0)
We get the same result as above.
You might also be interested in –
- Python – Convert a deque of strings to a single string
- Python – Convert a deque to a list
- Python – Get the Index of the Maximum Value in Deque
- Python – Get the Index of the Minimum Value in Deque
- Python – Get the Maximum and the Minimum Value in a Deque
- Python – Get Value by Index in Deque
- Python – Count occurrences of a value in deque
- Python – Create a deque copy
- Python – Rotate a Deque to the Right and Left
- Python – Extend deque to the left
- Python – Extend deque to the right
- Python – Get the Max Size of a Deque
- Python – Reverse a deque
- Python – Get Index of a Value in Deque
- Python – Remove Element by Value in Deque
- Python – Remove all elements from a deque (clear deque)
- Python – Append Element to a Deque
- Python – Append Element to the Left in Deque
- Python – Pop Element From Left in Deque
- Python – Pop Element From the End in Deque
- Python – Insert Element at a given Index in Deque
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts