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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  How to Draw a Triangle in Python Using turtle Module
  • 2.  Python Split Tuple into Multiple Variables
  • 3.  Using Python to Count Items in List Matching Criteria
  • 4.  Get Python Dictionary Keys as List
  • 5.  Convert String to List Using Python
  • 6.  Python atan – Find Arctangent and Inverse Tangent of Number
  • 7.  pandas groupby size – Get Number of Elements after Grouping DataFrame
  • 8.  Count Letters in Word in Python
  • 9.  Get Size of File in Python with os.path.getsize() Function
  • 10.  How to Write Pickle File to AWS S3 Bucket Using Python

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