Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create and Check Empty Dictionary in Python

  • 01 Apr, 2025
  • Com 0
Create and check an empty dictionary in Python

The efficient and Pythonic way to create an empty dictionary is using the “{}” literal notation.

Creating an empty dictionary

There is a dictionary object in memory, but it does not have data (key-value pairs). That does not mean it is None because None means nothing in memory, while an empty dictionary has an object.

empty_dict = {}

print(empty_dict)
# Output: {}

print(type(empty_dict))
# Output: <class 'dict'>

The “{}” literal is the fastest approach because Python’s bytecode directly interprets it, and it does not require a function call, which reduces the overhead. For simplicity and readability, this approach is more appropriate.

Using dict() constructor

Creating an empty dictionary using dict() constructor

Another way is to assign the dict() constructor without any arguments to a variable, which will result in an empty dictionary.

empty_dict = dict()

print(empty_dict)
# Output: {}

print(type(empty_dict))
# Output: <class 'dict'>

It is slower than the literal ({}) approach because it requires a function call.

Checking if a dictionary is empty

Check if a dict is empty in Python using explicit boolean check

The most optimal way to check if a dictionary is empty is to use an implicit boolean check using the “not” operator with an “if statement”.

Dictionaries are inherently truthy if they contain at least one key-value pair and falsy if empty; therefore, this approach is efficient. Since empty mechanisms are falsy, not empty mechanisms become truthy.

empty_dict = {}

if not empty_dict:
    print("The dictionary is empty")
else:
    print("The dictionary is not empty")

# Output: The dictionary is empty

If a dictionary contains a single element, if condition won’t be executed:

dict = {'name': 'Krunal'}

if not dict:
    print("The dictionary is empty")
else:
    print("The dictionary is not empty")

# Output: The dictionary is not empty

The time complexity is O(1), which is why it’s the fastest.

You can also use a simple if-else condition if you don’t want to use the not operator:

empty_dict = {}

if empty_dict:
    print("The dictionary is not empty")
    
else:
    print("The dictionary is empty")

# Output: The dictionary is empty

Using explicit length checking

An empty dictionary has a length of zero, and using the len() function, we can compare the dictionary’s length to zero to determine whether it is empty or not.

empty_dict = {}

if len(empty_dict) == 0:
    print("The dictionary is empty")
else:
    print("The dictionary is not empty")

# Output: The dictionary is empty

It is not as fast and efficient as an explicit Boolean check because here, we need to call a function, which adds a slight overhead.

Comparing a dictionary with {}

There is a third way, which involves directly comparing an empty dictionary with “{}”. However, this approach is not advisable since “{}” creates a new dictionary for comparison, consuming both memory and time. 

It has O(1) efficiency for checking an empty dictionary, but is inefficient for large dicts.

empty_dict = {}

if empty_dict == {}:
    print("The dictionary is empty")
else:
    print("The dictionary is not empty")

# Output: The dictionary is empty

If possible, please avoid this approach entirely.

That’s all!

Post Views: 113
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Create and Check Empty DataFrame in Pandas
How to Create and Check If a Numpy Array is Empty

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend