How to Check if Python Dictionary is Empty or Not?
Hi Dev,
Today, how to check dictionary is empty in python is our main topic. we will help you to give an example of python dictionary is empty or not. you will learn python dictionary is empty check. let us discuss about python check whether dictionary is empty. follow the below step for python 3 check if dictionary is empty.
There are several ways to check python dictionary is empty or not. i will give you simple three ways to check python dictionary is empty or not. so you can see the following example codes.
So, without further ado, let's see simple examples:
Example 1:
main.py
# Created empty Dictionary Object myDictionary = {} # Check Dictionary empty or not if myDictionary: print("Dictionary is not empty.") else: print("Dictionary is empty.")
Output:
Dictionary is empty.
Example 2:
main.py
# Created empty Dictionary Object myDictionary = {} # Check Dictionary empty or not if len(myDictionary) == 0: print("Dictionary is empty.") else: print("Dictionary is not empty.")
Output:
Dictionary is empty.
Example 3:
main.py
# Created empty Dictionary Object myDictionary = {} # Check Dictionary empty or not if any(myDictionary): print("Dictionary is not empty.") else: print("Dictionary is empty.")
Output:
Dictionary is empty.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Dictionary Check if Value Exists Example
- Python Dictionary Check if Key Exists Example
- How to Update Value in Python Dictionary?
- How to Change Value in Python Dictionary?
- Python Dictionary Remove Element by Value Example
- Python Dictionary Delete Item by Key Example
- How to Remove Element from Python Dictionary?
- How to Remove Item from Dictionary in Python?
- How to Add a Key Value Pair to Dictionary in Python?
- How to Add Element in Dictionary Python?
- How to Add Item in Dictionary Python?
- Python Create JSON File from Dict Example
- Python Generate Text File from List Example
- How to Add Element to a List in Python?