Check if Python Dictionary is Empty or Not Tutorial Example

10-Feb-2023

.

Admin

Check if Python Dictionary is Empty or Not Tutorial Example

Hi Dev,

This tutorial shows you check if python dictionary is empty or not. you'll learn how to check dictionary is empty or not in python. you can understand a concept of python 3 check if dictionary is empty. I explained simply about python check whether dictionary is empty. You just need to some step to done python dictionary is empty check.

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.

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.

#Python