• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Check if Deque is Empty

Using Python to Check if Deque is Empty

May 18, 2022 Leave a Comment

To check if a deque variable is empty in Python, you can check if the deque has length 0.

from collections import deque

d = deque()

print(len(d) == 0)

#Output:
True

You can also convert it to a boolean and check if the deque converted to a boolean is False.

from collections import deque

d = deque()

print(bool(d) == False)

#Output:
True

The deque object from the collections module gives us a data structure which is a double-ended queue.

With deque in Python, we get fast and memory-efficient ways to prepend, append, and remove items from a collection of data.

When working with deque in Python, the ability to check if a deque is empty can be important. This can be valuable because if you try to use the popleft() function on an empty deque, then you will raise a IndexError.

To check if a deque variable is empty in Python, you can check if the deque has length 0.

Below shows a simple example of how you can check if a deque object is empty with the len() function.

from collections import deque

d = deque()

print(len(d) == 0)

#Output:
True

You can also convert it to a boolean and check if the deque converted to a boolean is False.

Below shows a simple example of how you can check if a deque object is empty with the bool() function.

from collections import deque

d = deque()

print(bool(d) == False)

#Output:
True

Hopefully this article has been useful for you to learn how to check if a deque object is empty in Python

Other Articles You'll Also Like:

  • 1.  Fibonacci Sequence in Python with for Loop
  • 2.  How to Save and Use Styles from Existing Word File With python-docx
  • 3.  Python Factorial Recursion – Using Recursive Function to Find Factorials
  • 4.  Check if Variable is Float in Python
  • 5.  Python Round to Nearest 10 With round() Function
  • 6.  Python Prime Factorization – Find Prime Factors of Number
  • 7.  Get Quarter from Date in pandas DataFrame
  • 8.  Repeat String with * Operator in Python
  • 9.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 10.  islower Python – Check if All Letters in String Are Lowercase

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy