Sunday, December 29, 2024
Google search engine
HomeLanguagesHow to check if a deque is empty in Python?

How to check if a deque is empty in Python?

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)

RELATED ARTICLES

Most Popular

Recent Comments