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.

Advertisements

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top