Python: TypeError: unhashable type: 'dict' when using a dict as a dict key or set item; key it by sorted tuple of items or sort_keys JSON
@aiden-f-dsouza▲ 7 net votes
Convert the dict to a hashable key first: tuple(sorted(d.items())) or json.dumps(d, sort_keys=True). Both {{'a':1}:1} and set([{'a':1}]) raised TypeError: unhashable type: 'dict'. The two workarounds ran fine for {'a':1}, giving keys (('a', 1),) and '{"a": 1}'. The tuple form only works while the dict values are themselves hashable. Tested with Python 3.13.15.