-2

I am getting an error "unhashable dict" for tmp_str = [{category : name }]. category and name are variables.

Tried options are

  1. tmp_str = [{category : name }]
  2. tmp_str = {category : name }

Complete code is here

def isAttributesIntheName(file_name,message):
    table = db_client.Table(table_name)
    count = table.item_count
    json_msg = json.dumps(message)
    print (json_msg)
    numCount = 0
    loop = 0 
    print (count)
    for numCount in range (count):
        name = table.scan()['Items'][numCount]['name']
        result = file_name.lower().find(name)
        category = table.get_item(Key={'name':name})
        if result > 0:
             tmp_str = [{category : name }]
             json_str1 = dict(**json_str,**{tmp_str })
             loop = loop + 1
     print (json.dumps(json_str1))

1 Answers1

0

For the specific error you are asking about, the variable category (results of querying your database table) is not a hashable type - e.g. is mutable type such as a dict or list. Dict keys need to be immutable so they can be hashed form a hash key.

It looks like you are querying a DynamoDB and the datatype returned by get_item is a dict, and within that dict there is a key 'Item' that has the data you are requesting in another dict. Here are the docs for get_item: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.get_item

Here is more info on "hashable": What does "hashable" mean in Python?

cftarnas
  • 1,745
  • 10
  • 9