Python Update Empty Dictionary: 4 Ways & When to Use Each

How-to basics · Updated November 23, 2026

To update an empty dictionary in Python, you can use the update() method, direct key assignment, the ** unpacking operator, or dictionary comprehension. The most straightforward way is my_dict.update({'key': 'value'}) or simply my_dict['key'] = 'value' — both work immediately on an empty dict.

Background: What an Empty Dictionary Is

In Python, a dictionary is a mutable, unordered collection of key-value pairs. An empty dictionary is created with {} or dict(). It contains no items, but it is a fully functional object with all dictionary methods. Updating it simply means adding the first key-value pairs or merging another dictionary into it.

Unlike lists or strings, dictionaries are mutable in place, so updating an empty dictionary does not create a new object — it modifies the original. This is important for performance and for references: if you assign an empty dict to two variables and update one, the other also sees the change.

How Python’s update() Method Works

The update() method merges keys from another dictionary, an iterable of key-value pairs, or keyword arguments into the existing dictionary. If a key already exists, its value is overwritten; if not, it is added. For an empty dictionary, every key is new.

Syntax examples:

  • d.update({'a': 1}) — merges a dict literal.
  • d.update(a=1, b=2) — merges keyword arguments (keys become strings).
  • d.update([('a', 1), ('b', 2)]) — merges a list of tuples.

All of these work on an empty dictionary. The method returns None, so it modifies the dictionary in place rather than returning a new one.

Direct Assignment: The Simplest Way

For a single key, direct assignment is often clearer than update():

d = {}
d['name'] = 'Alice'
print(d)  # {'name': 'Alice'}

This is the most common way to add the first item to an empty dictionary. It is also faster than update() for a single key because it avoids the overhead of building a temporary dictionary or iterable.

For multiple keys, you can chain assignments or use update():

d = {}
d['x'] = 1
d['y'] = 2

Both approaches are idiomatic; choose based on readability and whether you have the data as a pre-existing mapping.

Using ** Unpacking and dict() Constructor

You can also create a new dictionary by unpacking an existing empty dict with other mappings:

d = {}
new = {**d, 'key': 'value'}
print(new)  # {'key': 'value'}

This does not modify d; it creates a new dictionary. It is useful when you want to avoid mutating the original, but for an empty dict you could just start with the literal.

The dict() constructor can also update, but it returns a new dict rather than updating in place:

d = {}
d = dict(d, key='value')  # works, but reassigns d

In practice, for an empty dictionary, most programmers use direct assignment or update() because they are explicit and efficient.

Common Mistakes and Pitfalls

One frequent mistake is using d = d.update(...) expecting a new dictionary. Since update() returns None, this sets d to None. Always call it without assignment.

Another pitfall is confusing an empty dictionary with an empty set. {} creates a dict, but set() creates an empty set. If you try to update a set with update(), it adds elements, not key-value pairs.

Also, remember that dictionary keys must be hashable. If you try to use a list as a key, you’ll get a TypeError. For an empty dict, this matters only when you start adding items, but it’s a common source of confusion.

When to Use Each Method: A Comparison

Here is a quick guide to choosing the right approach:

  • Single new key → use d[key] = value.
  • Merging another dict or iterable → use d.update(other).
  • Creating a new dict without mutating the original → use dict unpacking {**d, ...} or dict(d, ...).
  • Building a dict from a list of pairs → use d.update(list_of_pairs) or a comprehension with dict().

Performance-wise, direct assignment is the fastest for a single key. For merging large dictionaries, update() is implemented in C and is efficient. Unpacking creates a new dict, which uses more memory but is safe if you need immutability.

Alternatives: dict Comprehensions and setdefault()

If you need to build an empty dictionary from an iterable of keys with default values, a dictionary comprehension is concise:

keys = ['a', 'b', 'c']
d = {k: 0 for k in keys}
print(d)  # {'a': 0, 'b': 0, 'c': 0}

This is not an update per se, but it is a common way to initialize a dict that you will later update.

The setdefault() method is useful when updating an empty dict in a loop, because it sets a default value only if the key is missing:

d = {}
for word in ['apple', 'banana', 'apple']:
    d.setdefault(word, 0)
    d[word] += 1
print(d)  # {'apple': 2, 'banana': 1}

For an empty dict, setdefault() works exactly like assignment for the first occurrence, but it is more verbose for simple cases.

Frequently Asked Questions

Does update() work on an empty dictionary? Yes, update() works on any dictionary, including an empty one. It adds all key-value pairs from the argument.

What is the difference between d.update() and d[key] = value? update() can merge multiple items from a dict, iterable, or keyword arguments at once. Direct assignment adds or replaces a single key. For a single key, assignment is simpler and faster.

Can I update an empty dictionary with another empty dictionary? Yes, d.update({}) does nothing because there are no items to add. The dictionary remains empty.

Why does d.update() return None? It is designed to modify the dictionary in place, following Python’s convention for mutable objects that change in place. If you need a new dict, use dict unpacking or the dict() constructor.

Is there a way to update an empty dict with a list of keys and values? Yes, pass a list of tuples to update(): d.update([('a', 1), ('b', 2)]). Or use a dict comprehension with zip() if you have separate lists of keys and values.

What happens if I use a list as a key in an empty dict? You’ll get a TypeError: unhashable type: 'list'. Keys must be immutable and hashable, such as strings, numbers, or tuples.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.