Check If a Dictionary is Empty in Python

Checking if a dictionary is empty allows you to determine whether the dictionary has any key-value pairs or not. In this blog post, we will explore different methods to check if a dictionary is empty in Python.

Method 1: Using the len() Function

# Method 1: Using the len() Function
my_dict = {}
if len(my_dict) == 0:
    print("Dictionary is empty")
else:
    print("Dictionary is not empty")

Output:

Dictionary is empty

Method 2: Using the not operator

# Method 2: Using the not operator
my_dict = {}
if not my_dict:
    print("Dictionary is empty")
else:
    print("Dictionary is not empty")

Output:

Dictionary is empty