5 Ways to Check If a Dictionary is Empty in Python

5/5 - (4 votes)

Problem Formulation and Solution Overview

This article will show you how to check if a Dictionary is empty in Python.

Before moving forward, let’s review what a Python Dictionary is.

πŸ’‘Dictionary Overview: The Python Dictionary is a data structure that houses key:value pairs of data. This data structure does not allow duplicate keys, thus ensuring unique key data. An example of a Dictionary containing data is shown below.

students_grade5 = {'Amy': 11, 'Micah': 10, 'Chloe': 11}

As seen above, the value portion of the key:value pair allows duplicates.


πŸ’¬ Question: How would we write code to check if a Dictionary is empty?

We can accomplish this task by one of the following options:


Method 1: Use if/else statement

This example uses an if/else statement to check if a Python Dictionary is empty.

students_grade6 = {}

if (students_grade6):
    print('Dictionary Contains Data!')
else:
    print('Dictionary Empty!')

The above code declares an empty Dictionary and saves this to students_grade6. If output to the terminal, the following displays.

{}

Next, an if/else statement is created. The if statement checks the Dictionary. Does it contain any data, or is it empty? As you can see from the above output, the Dictionary is empty.

Therefore, the print statement inside the else statement executes.

Dictionary Empty!
Python Dictionary – The Ultimate Guide

Method 2: Use a One-Liner

This example uses a one-liner. Also known as the ternary operator to check if a Python Dictionary is empty.

students_grade6 = {'Mary': 13}
print('Dictionary Contains Data!') if students_grade6 else print('Dictionary Empty!')

The above code declares a Dictionary containing one (1) key:value pair and saves the contents to students_grade6. If output to the terminal, the following displays.

{'Mary': 13}

Next, a ternary operator is used to determine if students_grade6 contains data. The output is based on the outcome and sent to the terminal.

Dictionary Contains Data!
If-Then-Else in One Line Python

Method 3: Use len()

This example uses Python’s built-in len() function to check if a Python Dictionary is empty.

students_grade6 = {}

if len(students_grade6) == 0:
    print('Dictionary Empty!')

The above code declares an empty Dictionary and saves the contents to students_grade6. If output to the terminal, the following displays.

{}

Next, an if statement is declared, calls len(), and passes one (1) argument, students_grade6. This determines the length of the Dictionary.

Since the Dictionary is empty, the following is output to the terminal.

Dictionary Empty!
Python len() – A Simple Guide

Method 4: Use == Operator

This example uses Python’s == (equal to operator) to check if a Python Dictionary is empty.

students_grade6 = {}

if (students_grade6 == 0):
    print('Dictionary Empty!')

The above code declares an empty Dictionary and saves the contents to students_grade6. If output to the terminal, the following displays.

{}

Next, an if statement is declared and uses the == (equal to) operator to determine the length of the Dictionary students_grade6.

Since the Dictionary is empty, the following is output to the terminal.

Dictionary Empty!
Python Equal To Operator Deep Dive

Method 5: Use Boolean operator

This example is very similar to Method 2. However, this example wraps a variable inside the bool() function to check if a Python Dictionary is empty.

students_grade6 = {'Mary': 13}
print('Dictionary Contains Data!') if bool(students_grade6) else print('Dictionary Empty!')

The above code declares a Dictionary containing one (1) key:value pair and saves the contents to students_grade6. If output to the terminal, the following displays.

{'Mary': 13}

Next, a ternary operator is used to determine if students_grade6 contains data. The output is based on the outcome of bool(students_grade6) (True or False) and sends the result to the terminal.

Dictionary Contains Data!
Python bool() - Everything You Need to Know and More

Summary

This article has provided five (5) ways to check if a Python Dictionary is empty to select the best fit for your coding requirements.

Good Luck & Happy Coding!