How to Create a Python Empty Dict
python empty dict: Learn the correct ways to create an empty dictionary in Python, compare literal and constructor approaches, and avoid common pitfalls.
Creating a python empty dict is one of the first things you do when building data structures in Python. The syntax is minimal, but there are subtle differences between the available methods that affect readability, performance, and correctness in edge cases. This article covers the literal syntax, the dict() constructor, how to check emptiness, and the mistakes that often appear when dictionaries start empty and grow dynamically.
The Literal Syntax: {}
The most direct way to create an empty dictionary is the literal {}:
empty_dict = {}
This produces a dictionary with no key-value pairs. The literal syntax is the idiomatic choice in almost all Python code. It is fast, readable, and immediately signals that the variable is a dictionary. The same syntax is used to create non-empty dictionaries, so the empty case is consistent with the rest of the language.
config = {} config["debug"] = True
The literal creates a fresh dictionary object. Each assignment to {} creates a new object, so you can safely use it as a default value in function signatures only if you are careful about mutable defaults. The common workaround is to use None and initialize inside the function, but that is a separate concern from the creation itself.
Using the dict() Constructor
The dict() constructor also creates an empty dictionary:
empty_dict = dict()
This is functionally equivalent to {} for an empty dictionary. The constructor is more explicit, which can be useful in contexts where the type name improves clarity, such as when a variable name does not already indicate the type. For example, in a function that returns a mapping, return dict() might read slightly better than return {} if the return type is not obvious.
The constructor also accepts keyword arguments, iterables, and mapping objects, but with no arguments it simply returns an empty dictionary. There is no practical performance difference between {} and dict() for the empty case. Both allocate a new dictionary object with the same internal structure.
When to Choose {} Over dict()
In idiomatic Python, the literal {} is preferred. The Python style guide and most codebases use the literal for empty dictionaries because it is shorter and avoids a function call. The constructor is useful when you need to convert other data structures, but for an empty dictionary the literal is the standard.
There is one subtle difference: {} is a syntax construct, while dict() is a name lookup. In the rare case that the built-in dict name is shadowed in a scope, {} still works, but dict() would raise a NameError or call a different function. This is rarely a practical concern, but it is worth knowing if you work in code that redefines built-ins.
Checking Whether a Dictionary Is Empty
Once you have an empty dictionary, you often need to test whether it has gained any entries. The idiomatic way is to use the dictionary directly in a boolean context:
if not my_dict: print("Dictionary is empty") else: print("Dictionary has items")
An empty dictionary is falsy, so not my_dict evaluates to True when there are no keys. This is the most readable and efficient check. Using len(my_dict) == 0 is equivalent but slightly more verbose. The boolean check is preferred because it directly expresses the intent and avoids an extra function call.
For code that needs to be explicit, len() is also acceptable:
if len(my_dict) == 0: # handle empty case
Both approaches are correct. The boolean check is more Pythonic and is what you will see in most professional code.
Common Mistakes When Working with Empty Dictionaries
A frequent error is confusing {} with set(). The expression {} creates an empty dictionary, not an empty set. To create an empty set, you must use set(). This confusion arises because the set literal syntax {1, 2, 3} uses curly braces, but the empty set has no literal form.
empty_set = set() # correct wrong_set = {} # this is a dict, not a set
Another mistake is using an empty dictionary as a default argument in a function definition:
def add_item(item, cache={}): cache[item] = True return cache
The default dictionary is created once when the function is defined, not on each call. All calls share the same dictionary, which leads to unexpected state accumulation. The correct pattern is to use None and create a fresh dictionary inside the function:
def add_item(item, cache=None): if cache is None: cache = {} cache[item] = True return cache
This avoids the shared-mutable-default problem and is a common source of bugs in production code.
Performance and Memory Considerations
Creating an empty dictionary is a low-cost operation. The literal {} and dict() both allocate a new object with an initial internal table. The memory footprint of an empty dictionary is small but not zero; it reserves space for a few entries before resizing. If you plan to add many items, the dictionary will resize automatically, which is expected behavior.
There is no meaningful performance difference between {} and dict() for the empty case. The literal is marginally faster because it avoids a name lookup, but the difference is negligible in real-world code. The choice should be driven by readability and consistency, not micro-optimization.
When you need to create many empty dictionaries in a loop, the same rules apply. Each iteration creates a new object, which is normal. If you find yourself repeatedly creating and discarding dictionaries, consider whether a different data structure or a single reused dictionary with cleared contents would be more efficient, but only after profiling. Premature optimization is rarely justified for dictionary creation.
Empty Dictionaries in Larger Codebases
In larger applications, empty dictionaries often serve as accumulators, caches, or configuration stores. The way you create them should be consistent across the codebase. Most teams adopt the literal {} as the standard for empty dictionaries and reserve dict() for cases where the constructor is actually needed, such as converting from another mapping type.
Type hints also interact with empty dict creation. When a variable is annotated as dict[str, int], assigning {} is valid and the type checker understands the empty literal as a dictionary. For example:
counts: dict[str, int] = {}
This is clearer than dict() because the type annotation already specifies the structure. In typed code, the literal is the natural choice.
Another consideration is copying. If you need an independent empty dictionary based on an existing one, use .copy() on the existing dictionary or simply create a new literal. The copy method preserves the type and any subclass behavior, which can matter when working with custom dictionary subclasses.
original = {} copy = original.copy()
For most cases, {} is sufficient. Understanding when to use the literal versus the constructor, how to test emptiness, and how to avoid mutable default arguments will prevent the most common dictionary-related bugs in Python code.