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()
methodIf
statementNot
operatorlen()
method
In this article, I will explain using the 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 Checking If a Dictionary is Empty
If you are in a hurry, following are the quick examples of checking if a dictionary is empty. If the dictionary is empty.
# Quick examples of checking if a dictionary is empty
# Example 1: Check if the dictionary is empty using bool() method
my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
print("If the dictionary is empty:", bool(empty_dict))
print("If the dictionary is not empty:", bool(dict))
# Example 2: Check if the dictionary is empty using if statement
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
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 the Python Dictionary is Empty using bool()
You can use the bool() function to check if the dictionary is empty or not. This function is used to convert value to boolean value. It takes a single argument and returns either True or False. If the passed argument is an empty dictionary it will return False
or not, it will return True
.
# Check if the dictionary is empty using bool() method
my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
print("Empty dictionary:", empty_dict)
print("If the dictionary is empty:", bool(empty_dict))
print("Given dictionary:", my_dict)
print("If the dictionary is not empty:", bool(my_dict))
Yields below output.
4. Use the 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 otherwise else
part will be executed.
In this example, I will create two dictionaries one is an empty dictionary and another one is a dictionary with key/value pair. For the first time, you can apply if
condition on my_dict
and second time apply on empty_dict
.
# Check if the dictionary is empty using if statement
my_dict = {"course": "Python", "fee": 4000, "duration": "45 days"}
empty_dict = {}
print("Given dictionary:", my_dict)
if my_dict:
print("Dictionary is not empty")
else:
print("Dictionary is empty")
print("Empty dictionary:", empty_dict)
if empty_dict:
print("Dictionary is not empty")
else:
print("Dictionary is empty")
Yields below output.
5. To Check If the Dictionary is Empty using not Operator
You can use the not
operator over the empty dictionary and check if it is empty or not. This boolean operator takes a 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("Given dictionary:", empty_dict)
print("Dictionary is empty:", not empty_dict)
# Output:
# Given dictionary: {}
# Dictionary is empty or not: True
6. To Check If the Dictionary is Empty using the len() method
Alternatively, you can use the len() method to check if the dictionary is empty or not. This function returns 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
empty_dict = {}
dict = len(empty_dict)
print("Given dictionary:", empty_dict)
if dict == 0:
print ("Dictionary is empty")
else:
print ("Dictionary is not empty")
# Output:
# Given dictionary: {}
# 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 with examples.
Happy learning!!
Related Articles
- Get the Index of Key in Python Dictionary
- Python Dictionary len() Function
- Iterate Python Dictionary using enumerate() Function
- Append Python Dictionary to Dictionary
- Get Python Dictionary Values as List
- Convert Python Dictionary to JSON
- How to Add Items to a Python Dictionary
- Sort Python Dictionary Explained with Examples
- Python Dictionary update() Method
- Python Dictionary fromkeys() Usage With Example
- Python Dictionary copy()
- Python Dictionary keys() Method Usage
- Python if else Statement with Examples
- Python if __name__ == “__main__”: Explain?
- Python Nested if else Statement Examples