How to check if a Python dictionary is empty. You can check if the dictionary is empty or not using the logical method, logical operator, and if statement of Python. You can create an empty dictionary very easily using curly braces({}) and dict() constructer. Likewise, you can check if the dictionary is empty or not using the following methods and operators very easily.
bool() method
If statement
Not operator
len() method
In this article, I will explain using bool() method, boolean operator(not), if statement, and len() method how we can check if the dictionary is empty or not with examples.
1. Quick Examples of Check If a Dictionary is Empty
If you are in hurry, following are the quick examples of check if a dictionary is empty.
# Quick examples of check if a dictionary is empty
# Example 1: Check if the dictionary is empty using bool() method
dict = {“course” : “Python”}
empty_dict = {}
print(“If empty_dict is empty:”, bool(empty_dict))
print(“If dict is not empty:”, bool(dict))
# Example 2: Check if the dictionary is empty using if statement
my_dict = {“course”: “Python”, “fee”: 4000, “duration”: “45 days”}
empty_dict = {}
if my_dict:
print(“Dictionary is not empty”)
else:
print(“Dictionary is empty”)
# Example 3: Check if the dictionary is empty using not operator
empty_dict = {}
print(“Dictionary is empty:”, not empty_dict)
# Example 4: Check if the dictionary is empty using len() method
# my_dict = {“course”: “Python”, “fee”: 4000, “duration”: “45 days”}
empty_dict = {}
dict = len(empty_dict)
if dict == 0:
print (“Dictionary is empty”)
else:
print (“Dictionary is not empty”)
What is Python Dictionary
A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys.
Moreover, dictionaries are mutable data types, which means that you can be added, deleted, and updated their key:value pairs.
A value can be any data type such as a number(int, float), a string, a list, a tuple, or even another dictionary and it allows duplicates. On the other hand, keys are immutable types such as string, numbers(int, float), and tuple and it doesn’t allow duplicates. In case, you add duplicates, which will update the existing keys of the Python dictionary.
2. Syntax of Dictionary
Python dictionary is a collection of key-value pairs enclosed with {}, each key and its corresponding value separated by a colon (:).
# Syntax of the dictionary
courses = { “course”: “python”,
“fee”: 4000,
“duration”: “30 days”}
3. Check If he Python Dictionary is Empty using bool()
You can use bool() function to check if the dictionary is empty or not. The bool() function of Python is used convert value to boolean value. It take single argument and return either True or False. If the passed argument is empty dictionary it will return False or not, it will return True.
# Check if the dictionary is empty using bool() method
dict = {“course” : “Python”}
empty_dict = {}
print(“If empty_dict is empty:”, bool(empty_dict))
print(“If dict is not empty:”, bool(dict))
# Output:
# If empty_dict is empty: False
# If dict is not empty: True
Yields below output.
4. Use If statement to Check the Dictionary is Empty
In Python if statement is used to check if the dictionary is empty or not. If the dictionary is empty if part will be executed other wise else part will be executed.
In this example, I will create two dictionaries one is empty dictionary and another one is dictionary with key/value pair. First time I will apply if condition on my_dict and second one apply if condition on empty_dict.
# Check if the dictionary is empty using if statement
my_dict = {“course”: “Python”, “fee”: 4000, “duration”: “45 days”}
empty_dict = {}
if my_dict:
print(“Dictionary is not empty”)
else:
print(“Dictionary is empty”)
if empty_dict:
print(“Dictionary is not empty”)
else:
print(“Dictionary is empty”)
# Output:
# Dictionary is not empty
# Dictionary is empty
Yields below output.
5. To Check If the Dictionary is Empty using not Operator
In Python, you can use the not operator over the empty dictionary and check if it is empty or not. not operator is a boolean operator and it takes single operand and returns the boolean value True if the operand is False and vice versa.
The boolean value of the empty dictionary is evaluated as False when we apply the not operator over the empty dictionary, which returns a negation of False that is True.
# Check if the dictionary is empty using not operator
empty_dict = {}
print(“Dictionary is empty:”, not empty_dict)
# Output:
# Dictionary is empty or not: True
6. To Check If the Dictionary is Empty using len() method
Alternatively, you can use the len() method to check if the dictionary is empty or not in Python. Using the len() function we can get the length of the object in Python. Let’s check the length of the empty dictionary is ‘0‘ using the if condition. If it is ‘0’ it will print dictionary is empty, otherwise, it will print dictionary is not empty.
# Check if the dictionary is empty using len() method
# my_dict = {“course”: “Python”, “fee”: 4000, “duration”: “45 days”}
empty_dict = {}
dict = len(empty_dict)
if dict == 0:
print (“Dictionary is empty”)
else:
print (“Dictionary is not empty”)
# Output:
# Dictionary is empty
7. Conclusion
In this article, I have explained using bool() method, boolean operator(not), if statement, and len() method how we can check if the dictionary is empty or not.
Happy learning!!
How to check if a Python dictionary is empty. You can check if the dictionary is empty or not using the logical method, logical operator, and if statement of Python. You can create an empty dictionary very easily using curly braces({}) and dict() constructer. Likewise, you can check if the dictionary is empty or not Read More Python, Python Tutorial, Pending review by admin, Python Dictionary Examples