How to check if a deque is empty in Python?
Last Updated :
13 Feb, 2023
In this article, we are going to know how to check if a deque is empty in Python or not.
Python collection module provides various types of data structures in Python which include the deque data structure which we used in this article. This data structure can be used as a queue and stack both because it allows the user to insert and delete the elements from the front and end that's why It is also called a double-ended queue.
It is necessary to check whether a data structure like linked list, deque, tree, etc. is empty or not before popping out the elements to make our code error-free. Because when we try to remove any element from an empty deque we get an Index Error. Let's see it with an example.
Python3
# Import deque
from collections import deque
# Initialize an emppty deque
deque1 = deque()
# Removing an element from deque
deque1.pop()
Output:
Traceback (most recent call last):
File "/home/01a7ab0c685202c4f679846e50b77e8d.py", line 8, in <module>
deque1.pop()
IndexError: pop from an empty deque
In the above code, we are importing a deque from the collections module and then initializing an empty deque. after that, we are trying to remove an element from an empty deque which results in an Index Error as seen in the output.
Time complexity: O(1)
Auxiliary space: O(1)
Let's learn how to check whether the deque is empty or not.
Example 1:
Use len() function to find whether the deque is empty or not.
Python3
# import deque
from collections import deque
# create an empty deque
deque1 = deque()
if len(deque1) == 0:
print("deque is Empty")
Output:
deque is Empty
In the above code, we are importing a deque using the collections module creating an empty deque after that checking whether the deque is empty or not using len() function which returns the length of the deque if the length of the deque is equal to "0" it will print "deque is empty".
Time complexity: O(1)
Auxiliary space: O(1)
Example 2:
Using the bool() method, we can check whether the deque is empty or not. The bool() method converts the deque into a Boolean and if the value is False it indicates the deque is empty.
Python3
# import deque
from collections import deque
# create an empty deque
deque1 = deque()
if bool(deque1) == False:
print("deque is Empty")
Output:
deque is Empty
In this example, we are converting deque into a boolean explicitly if a deque has data it converts to True and if deque is empty it converts to False.
Time complexity: O(1)
Auxiliary space: O(1)
Example 3:
Python3
# Import deque
from collections import deque
# Initialize a list
deque1 = deque(["Geeks","for","Geeks"])
# Running a infinite loop
while(True):
# Check if deque is empty or not
if deque1:
print(deque1)
deque1.pop()
else:
print("Deque is empty")
# Break the loop if deque
# became empty
break
Output:
deque(['Geeks', 'for', 'Geeks'])
deque(['Geeks', 'for'])
deque(['Geeks'])
Deque is empty
In the above code, We are initializing a deque with some data in it and then running an infinite while loop. Inside a loop, we are checking whether the deque is empty or not in every iteration if the deque is not empty pop an item from the deque else print the message "Deque is empty" and break the loop. In this example, if statement implicitly converts the deque into a boolean if the deque is not empty it converts True and if the deque is empty it converts to False.
Time complexity: O(n)
Auxiliary space: O(1)
Similar Reads
How to check if a csv file is empty in pandas Reading CSV (Comma-Separated Values) files is a common step in working with data, but what if the CSV file is empty? Python script errors and unusual behavior can result from trying to read an empty file. In this article, we'll look at methods for determining whether a CSV file is empty before attem
4 min read
How To Check If Cell Is Empty In Pandas Dataframe An empty cell or missing value in the Pandas data frame is a cell that consists of no value, even a NaN or None. It is typically used to denote undefined or missing values in numerical arrays or DataFrames. Empty cells in a DataFrame can take several forms:NaN: Represents missing or undefined data.N
6 min read
How to check if the PyMongo Cursor is Empty? MongoDB is an open source NOSQL database, and is implemented in C++. It is a document oriented database implementation that stores data in structures called Collections (group of MongoDB documents). PyMongo is a famous open source library that is used for embedded MongoDB queries. PyMongo is widely
2 min read
How to Check if PySpark DataFrame is empty? In this article, we are going to check if the Pyspark DataFrame or Dataset is Empty or Not. At first, let's create a dataframe Python3 # import modules from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, StringType # defining schema schema = StructType([ Struc
1 min read
Check if a list is empty or not in Python In article we will explore the different ways to check if a list is empty with simple examples. The simplest way to check if a list is empty is by using Python's not operator. Using not operatorThe not operator is the simplest way to see if a list is empty. It returns True if the list is empty and F
2 min read
How to check dataframe is empty in Scala? In this article, we will learn how to check dataframe is empty or not in Scala. we can check if a DataFrame is empty by using the isEmpty method or by checking the count of rows. Syntax: val isEmpty = dataframe.isEmpty OR, val isEmpty = dataframe.count() == 0 Here's how you can do it: Example #1: us
2 min read