ItSolutionStuff.com

How to Check if Python Dictionary is Empty or Not?

By Hardik Savani • October 30, 2023
Python

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...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Dictionary Check if Value Exists Example

Read Now →

Python Dictionary Check if Key Exists Example

Read Now →

How to Update Value in Python Dictionary?

Read Now →

How to Change Value in Python Dictionary?

Read Now →

Python Dictionary Remove Element by Value Example

Read Now →

Python Dictionary Delete Item by Key Example

Read Now →

How to Remove Element from Python Dictionary?

Read Now →

How to Remove Item from Dictionary in Python?

Read Now →

How to Add a Key Value Pair to Dictionary in Python?

Read Now →

How to Add Element in Dictionary Python?

Read Now →

How to Add Item in Dictionary Python?

Read Now →

Python Create JSON File from Dict Example

Read Now →

Python Generate Text File from List Example

Read Now →

How to Add Element to a List in Python?

Read Now →