Skip to content

python/setdefault-without-defaultdict

Rule setdefault-without-defaultdict
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

setdefault with an empty list, set or dict builds a new empty value on every call. Start the dict as a collections.defaultdict and index it directly.

from collections import defaultdict
def by_market(offers: list[Offer]) -> dict[str, list[Offer]]:
found: dict[str, list[Offer]] = {}
found: defaultdict[str, list[Offer]] = defaultdict(list)
for offer in offers:
found.setdefault(offer.market, []).append(offer)
found[offer.market].append(offer)
return found

The rule reports setdefault with an empty list, set or dict on a dict that the same function, or the module, starts as {} or dict() under a plain name. It leaves alone a dict that cannot start as a defaultdict there: a parameter, an attribute, a dict that another function returns, and a dict that starts with keys.

xpd has no automatic fix, so make the change by hand.

setdefault(key, []) builds a new empty list on every pass and throws it away when the key is there. The Python documentation says of defaultdict(list) that it is "simpler and faster than an equivalent technique using dict.setdefault()". On Python 3.15.2 on one machine, grouping 100,000 items under 50 keys took 3.87 milliseconds with setdefault, 3.75 with if key not in found, and 2.87 with a defaultdict. The table gives the defaultdict form of each call.

dict defaultdict
found.setdefault(key, []).append(value) defaultdict(list), then found[key].append(value)
found.setdefault(key, set()).add(value) defaultdict(set), then found[key].add(value)
found.setdefault(key, {})[name] = value defaultdict(dict), then found[key][name] = value

A defaultdict also adds a key when code reads one that is missing, so found[key] never raises a KeyError. Read with found.get(key) or test with key in found where a missing key matters, or return dict(found) to hand the caller a plain dict.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

$D can be a defaultdict. Start it with collections.defaultdict(list), or with set or dict for another kind of value, and write $D[$KEY] in place of this setdefault call, which builds a new empty value on every pass.

Browse the other rules of the Python pack.