Skip to Content

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?

check if a deque is empty in Python

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)

Output:

deque([])
deque([1, 2, 3])

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)

Output:

True
False

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)

Output:

True
False

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)

Output:

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)

Output:

True
False

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)

Output:

True
False

We get the same result as above.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.