In this tutorial, we will look at how to check if a dictionary is empty or not in Python with the help of some examples.
How to check if a dictionary is empty?

The dictionary data structure in Python is used to store key: value mappings. A dictionary is empty if it does not contain any such mappings. You can use the following methods to check if a dict is empty or not in Python.
- Using the
len()
function. - Comparing the dictionary with an empty dictionary.
- Using the dictionary in a boolean context.
Let’s now take a look at each of the above methods with the help of some examples.
Using the len()
function
The length of an empty dictionary is zero.
You can use the Python len()
function to calculate the length of the dictionary and then compare it with zero to check if it is empty or not. Here’s an example –
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# create two dictionaries - d1 empty and d2 non-empty d1 = {} d2 = {"Name": "Rahul", "Age": 23} # check if dictionary is empty print(len(d1)==0) print(len(d2)==0)
Output:
True False
We get True
as the output for the dict d1
as it is empty and False
for the dict d2
because it’s not empty (has non-zero length).
Note that using the curly braces, {}
without anything inside creates an empty dictionary.
Comparing the dict with an empty dict
You can also check if a dictionary is empty or not by comparing it with an empty dictionary using the ==
operator. Let’s look at an example.
# create two dictionaries - d1 empty and d2 non-empty d1 = {} d2 = {"Name": "Rahul", "Age": 23} # check if dictionary is empty print(d1 == {}) print(d2 == {})
Output:
True False
We get the same results as above.
Note that here we use {}
without anything inside to represent an empty dictionary. You can also use the dict()
constructor with default parameters to create an empty dict.
Dictionary in a boolean context
If you use a dictionary in a boolean context, it will evaluate to True
if it has any key: value pairs and it will evaluate to False
if it is empty. Thus, you can use the expression not d
to check if the dictionary d
is empty or not.
Here’s an example.
# create two dictionaries - d1 empty and d2 non-empty d1 = {} d2 = {"Name": "Rahul", "Age": 23} # check if dictionary is empty print(not d1) print(not d2)
Output:
True False
We get the same result as above. True
for the dict d1
as it’s empty and False
for the dict d2
as it’s not empty (d2
has two key: value pairs).
In this tutorial, we looked at three methods to check if a dictionary is empty or not. Use the method that you are the most comfortable with.
You might also be interested in –
- Python – Check If a List is Empty – With Examples
- Python – Check If Set Is Empty – With Examples
- Check If a Tuple Is Empty in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.