• 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 / How to Check if a Dictionary is Empty in Python

How to Check if a Dictionary is Empty in Python

February 9, 2022 Leave a Comment

We can easily check if a dictionary is empty in Python. An empty dictionary has length 0, and is equal to False, so to check if a dictionary is empty, we can just check one of these conditions.

empty_dict = {}

#length check
if len(empty_dict) == 0:
    print("Dictionary is empty!")

#if statement check
if empty_dict:
    print("Dictionary is empty!")

#comparing to empty dictionary
if empty_dict == {}:
    print("Dictionary is empty!")

In Python, dictionaries are a collection of key/value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily determine if the dictionary is empty.

There are a few ways you can determine if a dictionary is empty.

Of course, you can always test to see if the dictionary is equal to another empty dictionary. Second, the length of an empty dictionary is 0. Finally, when converting an empty dictionary to a boolean value, we get False.

In this case, we can use any one of these conditions to determine if a dictionary is empty or not.

In the following Python code, you can see the three ways you ca check if a dictionary is empty or not.

empty_dict = {}

#length check
if len(empty_dict) == 0:
    print("Dictionary is empty!")

#if statement check
if empty_dict:
    print("Dictionary is empty!")

#comparing to empty dictionary
if empty_dict == {}:
    print("Dictionary is empty!")

Checking if Dictionary is Empty with if Statement in Python

One fact we can use in Python to check if a dictionary is empty is that a dictionary that is empty is equivalent to the boolean value False.

In this case, we can test if a dictionary is empty using a simple if statement.

empty_dict = {}

#if statement check
if empty_dict:
    print("Dictionary is empty!")

Checking if Dictionary is Empty Using Python len() Function

One of the ways we can easily check if a dictionary is empty in Python is with the Python len() function.

The length of a dictionary which is empty is 0.

Checking to see if a dictionary is empty using the Python len() function is shown in the following Python code.

empty_dictionary = {}

if len(empty_dictionary) == 0:
    print("Dictionary is empty!")

Checking if Dictionary is Empty By Comparing to Another Empty Dictionary in Python

You can also check if a dictionary is empty by comparing it to another empty dictionary. This is the most obvious method and works if you want to check if a list is empty, or check if a set is empty.

Below is how to compare an empty dictionary to another dictionary to determine if the other dictionary is empty or not.

empty_dict = {}

if empty_dict == {}:
    print("Dictionary is empty!")

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

Other Articles You'll Also Like:

  • 1.  Print Approximately Equal Symbol in Python
  • 2.  Get Year from Date in pandas DataFrame
  • 3.  How to Shutdown Computer with Python
  • 4.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 5.  Pascal’s Triangle in Python
  • 6.  Using Python to Check if Queue is Empty
  • 7.  Writing Multiple Lines Lambda Expression in Python
  • 8.  Count Values by Key in Python Dictionary
  • 9.  How Clear a Set and Remove All Items in Python
  • 10.  Python math.factorial() function – Calculate Factorial of Number

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