In Python, dictionaries are mutable, which means you can add new key-value pairs to them at any time after creation. There are several ways to do this depending on whether you’re adding one key or several at once, and Python offers built-in tools for both simple and more advanced scenarios.
Adding a Single Key with Bracket Notation
The simplest and most common way to add a new key is by assigning a value to it using square bracket notation.
d = {"a": 1, "b": 2}
d["c"] = 3
print(d)
This outputs {'a': 1, 'b': 2, 'c': 3}. If the key already exists, this same syntax overwrites its value instead of creating a duplicate.
Adding Multiple Keys with update()
When you need to add several key-value pairs at once, the update() method is the standard approach. It accepts another dictionary, an iterable of key-value pairs, or keyword arguments.
d = {"a": 1, "b": 2}
d.update({"c": 3, "d": 4})
print(d)
This produces {'a': 1, 'b': 2, 'c': 3, 'd': 4}. You can also call it with keyword arguments directly, like d.update(e=5, f=6), for simple string keys.
Merging with the | Operator
Since Python 3.9, the | operator lets you merge dictionaries or add new keys without modifying the original.
d = {"a": 1, "b": 2}
res = d | {"c": 3}
print(res)
This creates a new dictionary containing the merged result while leaving d untouched — useful when you want to preserve the original data.
Adding a Key Only If It’s Missing with setdefault()
The setdefault() method adds a key with a given value only if that key doesn’t already exist. If the key is already present, it simply returns the existing value without changing it.
settings = {"theme": "dark"}
settings.setdefault("language", "en")
print(settings)
This is handy when you want to fill in default values without accidentally overwriting something already set.
Using defaultdict for Automatic Key Creation
If you’re frequently adding keys with default values, Python’s collections.defaultdict can save you from writing repetitive existence checks. It automatically creates a key with a default value the first time it’s accessed.
from collections import defaultdict
d = defaultdict(int)
d["count"] += 1
print(d)
This avoids KeyError issues that would otherwise occur when incrementing a key that doesn’t exist yet.
Which Method Should You Use?
For a single key, bracket assignment is the clearest and fastest choice. For bulk additions, update() or the | operator both work well, with | being preferable when you don’t want to mutate the original dictionary. Use setdefault() or defaultdict when you’re dealing with default values or want to avoid overwriting existing keys accidentally.
Join The Discussion
Dictionaries are one of the most-used data structures in Python, and there’s usually more than one way to get the job done. Do you have a preferred method for adding keys, or a scenario where one approach clearly beat the others in your own code? Whether you’re new to Python dictionaries or you’ve been working with them for years, share your experiences, edge cases, or questions below — what’s tripped you up, and what’s your go-to technique?