What does it mean when you hear an empty dictionary? An empty dictionary does not have key-value pairs. 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.
Creating an empty dictionary
The efficient and Pythonic way to create an empty dictionary is using the “{}” literal notation.
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
Another way is to assign a dict() constructor without any arguments to a variable, which will become 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
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, so it is an efficient approach. 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), and that’s why it is 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 zero length, and using the len() function, we can compare the dictionary’s length to 0 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 includes directly comparing an empty dictionary with “{}”, which is not a good idea since “{}” will create a new dictionary for comparison, which takes memory as well as 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.
Conclusion
For creating an empty dict, use the “{}” literal, and for checking an empty dict, use an explicit boolean check using the “if not empty_dict” expression.