Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Dictionary Default Value Techniques

Learn how to handle missing Python dictionary keys with get(), setdefault(), and defaultdict, including when each approach stores the default and when the default expression is evaluated.

~6 min readPython
#Python#dictionary#defaultdict#get method
Illustration of a Python dictionary with a default value fallback mechanism

When working with Python dictionaries, a common requirement is to retrieve a value for a key that may not exist. Python provides several built-in ways to supply a fallback for a missing key: get(), setdefault(), and collections.defaultdict. They differ in whether the fallback is stored back into the dictionary and when the default value is evaluated.

Using the get() Method for a One-Time Default

The simplest way to supply a default value for a missing key is the get() method. It returns the value for the given key if it exists; otherwise it returns a specified default (or None if no default is provided).

python
inventory = {"apples": 10, "bananas": 5} count = inventory.get("oranges", 0) print(count) # 0

This is ideal when you only need the value once and do not need to modify the dictionary. The original dictionary remains unchanged, and the default is computed eagerly—the expression for the default is evaluated even if the key exists. If the default is expensive to construct, that cost is paid on every call, which can matter in tight loops.

Inserting a Default with setdefault()

When you want to insert a default value into the dictionary only if the key is absent, setdefault() is the direct tool. It returns the existing value if the key is present; otherwise it inserts the default and returns it.

python
settings = {} mode = settings.setdefault("mode", "auto") print(mode) # auto print(settings) # {'mode': 'auto'} mode = settings.setdefault("mode", "manual") print(mode) # auto (unchanged)

This is useful for initializing mutable structures like lists or sets before updating them. For example, building a mapping from a word to a list of positions:

python
positions = {} for i, char in enumerate("hello"): positions.setdefault(char, []).append(i)

Note that the default argument is evaluated every time, so a mutable default like [] is created fresh on each call. This is safe, unlike default arguments in function definitions.

Automatic Defaults with collections.defaultdict

For scenarios where every missing key should automatically receive a default value, defaultdict from the collections module is the most concise solution. You supply a factory function that produces the default when a key is missing and is accessed with [].

python
from collections import defaultdict word_count = defaultdict(int) for word in ["apple", "banana", "apple"]: word_count[word] += 1 print(word_count) # defaultdict(<class 'int'>, {'apple': 2, 'banana': 1})

Accessing a missing key with defaultdict does not raise a KeyError; it calls the factory, stores the result, and returns it. This works well for counting, grouping, and accumulating data. The factory can be any callable: list, set, dict, or a custom function.

One important caveat is that defaultdict supplies a default only when a missing key is accessed through []. Methods like get() do not trigger the factory, so they still return None for missing keys unless you pass an explicit default. The in operator also does not insert a default.

Choosing Between get(), setdefault(), and defaultdict

The right choice depends on whether you need to persist the default and how often you access missing keys.

ApproachModifies dictionary?Default evaluated when?Best for
get()NoEvery callOne-off reads without mutation
setdefault()Yes, only if missingEvery callInitializing a single value before update
defaultdictYes, on missing keyOnly when key missingRepeated access with automatic defaults

Use get() when you only need a fallback for a single lookup and want to keep the dictionary unchanged. Use setdefault() when you need to insert a default before modifying a value, especially for mutable containers. Use defaultdict when the dictionary should always provide a default for any missing key, reducing boilerplate in loops that accumulate data.

Performance and Runtime Considerations

Each approach evaluates the default differently. get() and setdefault() always evaluate their default argument, even when the key is present. If the default is a simple constant like 0 or [], the overhead is negligible. If the default is a function call or a complex object, that work is wasted on every existing-key lookup.

defaultdict avoids that waste by only calling the factory when a key is actually missing. This makes it more convenient in scenarios with repeated accesses to missing keys. Its behavior is implemented through dict.__missing__, so it differs from a plain dict only when a missing key is accessed with []; concrete speed differences depend on the workload and Python version.

Another subtlety is that defaultdict changes the behavior of membership tests. The in operator does not create a default; it only checks existence. So key in dd is safe and does not modify the dictionary. But using dd[key] for a missing key does modify it, which can be surprising if you intended a read-only lookup.

Common Pitfalls and Edge Cases

A frequent mistake is assuming get() with a default will insert that default into the dictionary. It does not. If you need the dictionary to reflect the default, use setdefault() or defaultdict.

Another edge case involves None as a legitimate stored value. If a key exists with a value of None, get() returns None, which is indistinguishable from a missing key unless you explicitly check membership. This can lead to subtle bugs when None is a valid value.

python
d = {"a": None} print(d.get("a", "default")) # None, not "default"

If you need to distinguish between a missing key and a key with None, use in or a sentinel object.

For defaultdict, the factory is called once for each missing key that is accessed with [], so a factory like list gives each new key its own fresh list. If the factory returns an existing object, such as a lambda that captures a mutable value, then every missing key will share that same object. This sharing can be surprising, so make sure the factory creates a new object when that is what you need.

A Practical Example: Grouping Items by Category

Consider a function that groups a list of items by their category. Using defaultdict makes the code compact and avoids explicit initialization checks.

python
from collections import defaultdict def group_by_category(items): grouped = defaultdict(list) for item in items: grouped[item["category"]].append(item["name"]) return grouped items = [ {"category": "fruit", "name": "apple"}, {"category": "fruit", "name": "banana"}, {"category": "vegetable", "name": "carrot"}, ] print(dict(group_by_category(items))) # {'fruit': ['apple', 'banana'], 'vegetable': ['carrot']}

defaultdict records category keys in insertion order, so the output preserves the first-seen order in Python 3.7+. If downstream code needs a plain dict, convert the result with dict(grouped).

Handling Mutable Defaults Correctly

When the default value is a mutable object, such as a list or a set, you must ensure each missing key gets a fresh instance. With defaultdict, the factory handles this naturally. With get() and setdefault(), you must supply a new object each time.

python
# Correct with setdefault d = {} d.setdefault("k", []).append(1) # Correct with defaultdict dd = defaultdict(list) dd["k"].append(1)

A common error is to use a mutable default argument in a function definition, but that is unrelated to dictionary methods. Inside a dictionary, the default expression is evaluated each time, so d.get("k", []) creates a new list each call, which is safe.

When get() and setdefault() Fall Short

There are cases where the built-in methods do not provide the exact behavior you need. For instance, if you want to return a default without inserting it, but the default is expensive to compute and you want to avoid computing it when the key exists, you can use a conditional expression:

python
value = d["key"] if "key" in d else compute_default()

This evaluates compute_default() only when the key is missing. get() would evaluate it unconditionally. For most code, the difference is negligible, but in performance-critical paths it can matter.

Because defaultdict is a dict subclass, it works in most places a plain dict is expected. If you specifically need to return a plain dict, convert with dict(dd), which copies the entries. If you want custom default behavior without that copy, you can subclass dict and override __missing__, the same hook that defaultdict uses.

python
class DefaultDict(dict): def __missing__(self, key): return 0 d = DefaultDict() print(d["missing"]) # 0

This gives you full control over the default behavior, including the ability to return a default without inserting it into the dictionary. It is a more advanced pattern that is rarely necessary but useful when you need custom logic.

Understanding the nuances of each approach ensures you pick the right tool for the task, avoiding subtle bugs and unnecessary overhead in your Python code.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.