Search code snippets, questions, articles...

Check if dictionary is empty in Python

A dictionary can have multiple properties in Python. If a dictionary is empty and you want to check it before processing it then you can use the methods and statements explained in this post.
my_dict = {}

if not my_dict:
    print("Dictionary is empty")
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

Output

Dictionary is empty

Check if a dictionary is empty using 'not' keyword

This is the simplest and preferred method to check if a Python Dictionary is empty or not. We use the not keyword before the dictionary variable name and it will return true if the dictionary is empty and false if it is not empty.

Code example 1 - check for an empty dictionary

dict_a = {}

if not dict_a:
  print("Dictionary is empty")
else:
  print("Dictionary is not empty")

Output

Dictionary is empty

In the above code example:

  1. Created a dictionary named dict_a.
  2. Using if-else statement to check for empty dictionary.
  3. In the if statement - use the not keyword before the dictionary variable name and it will print - "Dictionary is empty" if the dictionary is empty.
  4. The code to check for an empty dictionary is not dict_a.

Code example 2 - check for a dictionary that has properties

subject = {
  "name": "Math",
  "score": 90
}

if not subject:
  print("Dictionary is empty")
else:
  print("Dictionary is not empty")

Output

Dictionary is not empty
  1. Created a dictionary named subject that has two keys - name and score in it.
  2. As described in the previous code example, we are also using here if-else statement along with not key keyword to check for empty dictionary.
  3. Here, 'Dictionary is not empty' message will be printed as output because the dictionary has some properties in it.

Check if dictionary is empty using len() function

We know that the Python len() function is used to get the length of a dictionary or list. So we can use it to check if a dictionary contains any property or not. If the function returns 0 then the dictionary is empty else it has some properties in it.

my_dict = {}

if len(my_dict) == 0:
  print("The dictionary is empty")
else:
  print("The dictionary is not empty")

Output

The dictionary is empty

Explanation of the above code example

  1. We have created a Python dictionary named my_dict.
  2. Using len(my_dict) function, we are calculating the length of the dictionary.
  3. We are using if-else statement and if len(my_dict) == 0, we are printing message - "The dictionary is empty" .

Check if dictionary is empty using bool() function

If we pass the dictionary variable to bool() function then it will return True if the dictionary is empty and False if the dictionary is not empty. So we will be using the not keyword with it to make it work as expected in the previous code examples.

my_dict = {}

if not bool(my_dict):
  print("The dictionary is empty")
else:
  print("The dictionary is not empty")

Output

The dictionary is empty

In the above code example

  1. We have created an empty dictionary named - my_dict.
  2. Passing the above dictionary to the bool() function and using not keyword with them.
  3. Print the message - "The dictionary is empty".
Was this helpful?
0 Comments