How to Check if a Dictionary is Empty in Python

Working with dictionaries in Python is exciting. However, sometimes you need to use a check for particular instances, such as if a dictionary is empty or not.

For example, you are working with a dataset to store inventory data. As an automation expert, you need to automate ordering inventory items if the stock runs out. In that case, you need to check if the dictionary with inventory data is empty. If it is empty, then you need to process the new orders.

So, how do you check if a dictionary is empty in Python? Before we start, let’s learn more about Python Dictionaries.

What are Python Dictionaries?

Python offers an excellent collection of built-in data structures. Dictionary is one of them that lets you store data in key: value pairs. This makes easy information-retrieval by accessing the key associated with the value.

In the Python dictionary, keys are unique and should be immutable data types, including numbers, strings, or tuples. Values can be of any data type, including a dictionary.

To create a dictionary, you can use any of the following codes.

empty_dictionary = {}
empty_dictionary_2 = dict()

If you try to print the dictionaries, you get an empty dictionary.

{}
{}

How to Check if a Dictionary is Empty in Python

Using if statement

One of the easiest ways to check if a Python dictionary is empty or not is using the if statement.

All you need to do is run the following command.

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

As empty_dictionary has no elements, the output will be as below.

The dictionary is empty

Check if a Python dictionary is empty or not using Boolean Method

Another interesting method is to use the bool() function. Python dictionary returns a Boolean value to False; otherwise, it will return True.

The code to check through the Boolean method is as below.

not_an_empty_dictionary = {"Nitish": 34}
empty_dictionary = {}
print("not_an_empty_dictionary is not empty --- ", bool(not_an_empty_dictionary))
print("empty_dictionary --- ", bool(empty_dictionary))

The result is as below.

not_an_empty_dictionary is not empty --- True
empty_dictionary --- False

As you can see, the dictionary with no items returns False, whereas the dictionary with at least one item returns True.

Check if a Python dictionary is empty or not using the Length Method

Python len() function lets you check the length of the data structure. In the case of the dictionary, it can count the number of keys and return its value. If the dictionary is empty, it will return 0; giving us the chance to use it to check if a dictionary is empty.

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

The output is:

The dictionary is empty
The dictionary is not empty

Here, we compared the dictionary length to zero as we already know that an empty dictionary will return the 0 value.

Comparing with an empty dictionary

Another counter-intuitive way to check if a given dictionary is empty or not is to compare it with an empty dictionary. { } denote an empty dictionary, and all we need to do is use the if statement and do the comparison. The code is as below:

empty_dictionary = {}
if empty_dictionary == {}:
    print("The dictionary is empty")
else:  
    print("The dictionary is not empty")

The code here is self-explanatory. We just used an empty dictionary { } and compared it with the given dictionary.

The output is as below:

The dictionary is empty

Using any() function

Python’s any() function checks if the input is iterative or not. If it is, it returns True, or else it returns False. This means if you pass a dictionary with no items, it should return False as there are no items to iterate.

Let’s see it in action.

#using the any function
empty_dictionary = {}
if any(empty_dictionary):
    print("The dictionary is not empty")
else:
    print("The dictionary is empty")

Using the not operator

Not operator works similar to the if statement. When used along with an empty dictionary, the not operator returns True.

The code is as below.

res = not empty_dictionary
print("Is the dictionary empty --- " + str(res))

The output is:

Is the dictionary empty --- True

Was this article helpful?

Leave a Comment