Python: Check if dictionary is empty

In this article we will discuss different ways to check if a dictionary is empty or not. These two ways to check if a python dictionary is empty or not,

Suppose we have two dictionaries:

The first dictionary contains strings as keys and integers as values and the second dictionary is empty,

# A Dictionary of string and integers
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

# An empty dictionary
sample_dict = {}

Now let’s see how we can check if the dictionary is empty or not.

Check if a dict is empty by casting dictionary to bool in if statement

In python we can cast or convert a dictionary to a bool variable. If we dictionary is empty then it will be True, otherwise False.

Now we can use this concept by putting a dictionary object in the if statement directly. If we pass the dictionary object in the if statement then it will be implicitly converted to a bool value. If the dictionary is empty then it will evaluate to True, otherwise False. Let’s understand with some examples,

Example 1:

# A Dictionary of string and integers
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

if word_freq:
    print('Dictionary is not empty')
else:
    print('Dictionary is empty')

Output:

Dictionary is not empty

As dictionary word_freq is not empty, so in the if condition the dictionary object evaluated to True and proved that the dictionary is not empty.

Example 2:

# An empty dictionary
sample_dict = {}

if sample_dict:
    print('Dictionary is not empty')
else:
    print('Dictionary is empty')

Output:

Dictionary is empty

As dictionary sample_dict is empty, so in the if condition the dictionary object evaluated to False and proved that the dictionary is empty.

Check if the dictionary is empty or not using len()

If we pass the dictionary object to the len() function, then it returns the number of key-value pairs in that dictionary. So, we can use this len() function to check if the dictionary is empty or not. Let’s understand this with some examples,

Example1:

# A Dictionary of string and integers
word_freq = {
    'Hello': 56,
    "at": 23,
    'test': 43,
    'This': 78,
    'Why': 11
}

if len(word_freq):
    print('Dictionary is not empty')
else:
    print('Dictionary is empty')

Output:

Dictionary is not empty

As dictionary word_freq is not empty, so len() function returned the total count of key-value pairs in the dictionary.  In this case word_freq contains 5 key-value pairs, so “if condition” evaluated to True and proved that the dictionary is not empty.

Example 2:

# An empty dictionary
sample_dict = {}

if len(sample_dict):
    print('Dictionary is not empty')
else:
    print('Dictionary is empty')

Output:

Dictionary is empty

As dictionary sample_dict is empty, so len() function returned the total count of key-value pairs in the dictionary.  In this case sample_dict contains zero key-value pairs, so “if condition” evaluated to False and proved that the dictionary is empty.

Conclusion:

Here we discussed two different ways to check if the dictionary is empty or not.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top