Typeerror: Unhashable Type: 'Dict'

TypeError(unhashable type: 'dict')

A valid dictionary key string should be enveloped by quotes or double quotes.

a_dict = {'key': 'value'}  # Valid
b_dict = {"key": "value"} # Valid

Or if you wish to assign string that was stored in a variable to be the dictionary key, you can do this instead:

st = "key"
a_dict = dict()
a_dict[st] = 'value'

Since json_dumps requires a valid python dictionary, you may need to rearrange your code.

If the l_user_type_data is a variable contains a string, you should do:

temp_dict = dict()
temp_dict[l_user_type_data] = user_type_data
result = json.dumps(temp_dict, default = date_handler)

Otherwise, if l_user_type_data is a string for the key, just simply enclose that with either single quote or double quotes.

return_data = json.dumps({                    
"l_user_type_data" : user_type_data
},default = date_handler)

How do I avoid a TypeError: unhashable type: 'dict' when computing list differences?

The set approach is not working since your dictionaries elements of lists. It seems that turning lists of dictionaries into sets is not allowed.

Instead, you can use a list comprehension where you check if any element in the list existing_dict is in the cur_dicts,

 deleted_dicts = [x for x in existing_dicts if not (x in cur_dicts)]

If the dictionary is not in cur_dicts, it is added to deleted_dicts. This relies on the fact that dictionaries can be compared for equality with an == operator.

Full example, extended with duplicate entries and larger dictionaries:

existing_dicts = [{"id": 1}, {"id": 2}, {"id": 2}, {"id": 2, "id2" : 3}, {"id": 1, "id2": 2}]
cur_dicts = [{"id": 2}, {"id": 1, "id2": 2}]

deleted_dicts = [x for x in existing_dicts if not (x in cur_dicts)]
print(deleted_dicts)

TypeError: unhashable type: 'dict' Removing duplicates from compex List structure

The solution from Remove duplicate dict in list in Python is not fully appliccable because you have inner lists.

You would need to tuplify them as well for usage in a set.

One way to do it would be:

data = [{'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']},
{'author': 'Sham Isa',
'authorId': 23,
'tags': ['badminton', 'game']},
{'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']}]

seen = set()
new_list = []
for d in data:
l = []
# use sorted items to avoid {"a":1, "b":2} != {"b":2, "a":1} being
# different when getting the dicts items
for (a,b) in sorted(d.items()):
if isinstance(b,list):
l.append((a,tuple(b))) # convert lists to tuples
else:
l.append((a,b))

# convert list to tuples so you can put it into a set
t = tuple(l)

if t not in seen:
seen.add(t) # add the modified value
new_list.append(d) # add the original value
print(new_list)

Output:

[{'author': 'Isav Tuco', 'authorId': 62, 'tags': ['fire', 'works']}, 
{'author': 'Sham Isa', 'authorId': 23, 'tags': ['badminton', 'game']}]

This is hacked though - you may want to get your own better solution.

Counter throws error: TypeError: unhashable type: 'dict'

original_list = [{'stock_id': 315, 'product_id': 315}, {'stock_id': 1, 'product_id': 1}, {'stock_id': 2, 'product_id': 2}, {'stock_id': 2, 'product_id': 2}, {'stock_id': 6, 'product_id': 6}]

intermediate_result = {}
for i in original_list:
if i["stock_id"] in intermediate_result.keys():
intermediate_result[i["stock_id"]] = intermediate_result[i["stock_id"]]+1
else:
intermediate_result[i["stock_id"]] = 1

result = []
for k,v in intermediate_result.items():
result.append({"stock_id": k, "count": v})

print(result)
[{'stock_id': 315, 'count': 1}, {'stock_id': 1, 'count': 1}, {'stock_id': 2, 'count': 2}, {'stock_id': 6, 'count': 1}]

About your code:

stock_ids = []
d = Counter(all_response)
for i in all_response:
# here is not okay. stock_ids is list of dictionaries
# so you comparing integer id with one of dictionaries
if i['stock_id'] not in stock_ids:
# here is half okay (you appending only id key, without count key)
stock_ids.append({'stock_id':i['stock_id']})
# here is not okay, because you trying to append new element to list
# you should append only once if you want only one dictionary element
stock_ids.append({'product_count': d[i['stock_id']]})

Command raised an exception: TypeError: unhashable type: 'dict'

I believe your problem is here:

for i in prints.json()['data']:
if prints.json()[i]['prices']['usd'] == 'null':

The variable "i" is a dict, as can be inferred from the other lines of code where you access some of its values by their keys. Just replace prints.json()[i] with i:

for i in prints.json()['data']:
if i['prices']['usd'] == 'null':


Related Topics



Leave a reply



Submit