TypeError: unhashable type: 'dict' (Python) — how to fix
Traceback (most recent call last):
File "app.py", line 4, in <module>
seen = {record}
TypeError: unhashable type: 'dict'
Quick fix: Don't put dicts in sets or use them as keys. Use a hashable stand-in instead: a specific field (seen.add(record['id'])), frozenset(d.items()), tuple(sorted(d.items())), or json.dumps(d, sort_keys=True) for nested dicts.
What it means
Sets and dictionary keys are stored by hash. A value is hashable only if it can never change, and dict is mutable, so hash({}) fails. Any operation that needs a hash of a dict raises this error:
d = {"id": 1}
{d} # set literal
s.add(d) # set.add
cache[d] = "x" # dict key
d in some_set # membership test on a set
The traceback line tells you which one. Look for {...}, set(...), .add(, [...] = on a dict, or a call to a function that hashes its arguments.
Cause 1: deduplicating a list of dicts with set()
rows = [{"id": 1}, {"id": 1}, {"id": 2}]
unique = set(rows) # TypeError
Fix: dedupe by a key field, keeping order:
seen = set()
unique = []
for r in rows:
if r["id"] not in seen:
seen.add(r["id"])
unique.append(r)
Or, if whole-dict equality matters and values are hashable:
unique = list({tuple(sorted(r.items())): r for r in rows}.values())
For nested dicts (values that are lists or dicts), serialize instead:
import json
unique = list({json.dumps(r, sort_keys=True): r for r in rows}.values())
Cause 2: using a dict as a dictionary key
counts = {}
counts[{"x": 1, "y": 2}] = 5 # TypeError
Use a tuple or frozenset of the items as the key:
key = frozenset({"x": 1, "y": 2}.items())
counts[key] = 5
Better still, use a meaningful immutable key such as (x, y), or a frozen dataclass:
from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
x: int
y: int
counts[Point(1, 2)] = 5
Cause 3: functools.lru_cache / cache with dict arguments
lru_cache hashes all arguments to build its cache key:
from functools import lru_cache
@lru_cache
def price(options): ...
price({"size": "L"}) # TypeError: unhashable type: 'dict'
Pass hashable arguments (price(size="L")), or convert before calling: price(frozenset(opts.items())) and rebuild with dict(options) inside.
Cause 4: pandas columns that contain dicts
Operations that hash cell values fail when a column holds dicts (common after loading JSON):
df.drop_duplicates() # TypeError: unhashable type: 'dict'
df["meta"].value_counts()
df.groupby("meta")
Flatten the JSON into columns, or compare on a string form:
df = pd.json_normalize(records) # expand nested dicts into columns
# or
df["meta_key"] = df["meta"].apply(lambda d: json.dumps(d, sort_keys=True))
df = df.drop_duplicates(subset=["id", "meta_key"])
Cause 5: a typo with braces
{data} creates a set containing data. If you meant to copy a dict, use dict(data) or {**data}. If you meant an f-string, the f prefix is missing.
To check exactly which value is the problem, print type(x) just before the failing line. If the value is JSON you can inspect it with the JSON formatter.
Last updated 2026-12-03.