How to Check if Python Dictionary is Empty or Not?

By Hardik Savani October 30, 2023 Category : Python

Hi Dev,

Today, how to check dictionary is empty in python is our main topic. we will help you to give an example of python dictionary is empty or not. you will learn python dictionary is empty check. let us discuss about python check whether dictionary is empty. follow the below step for python 3 check if dictionary is empty.

There are several ways to check python dictionary is empty or not. i will give you simple three ways to check python dictionary is empty or not. so you can see the following example codes.

So, without further ado, let's see simple examples:

Example 1:

main.py

# Created empty Dictionary Object
myDictionary = {}
  
# Check Dictionary empty or not
if myDictionary:
    print("Dictionary is not empty.")
else:
    print("Dictionary is empty.")

Output:

Dictionary is empty.

Example 2:

main.py

# Created empty Dictionary Object
myDictionary = {}
  
# Check Dictionary empty or not
if len(myDictionary) == 0:
    print("Dictionary is empty.")
else:
    print("Dictionary is not empty.")

Output:

Dictionary is empty.

Example 3:

main.py

# Created empty Dictionary Object
myDictionary = {}
   
# Check Dictionary empty or not
if any(myDictionary):
    print("Dictionary is not empty.")
else:
    print("Dictionary is empty.")

Output:

Dictionary is empty.

I hope it can help you...

Tags :
Shares