Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError("unhashable type: 'dict'")

I am trying to pass a data back to a URL fetch request. We are using Python 3.x

            user_type_data = {'user_type': 'admin', 
                'user_name': 'myname', 
                'user_check_flag': 'yes'}

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

            return return_data

When we do this for a dict I am getting the following error – TypeError("unhashable type: ‘dict’"). According to this, it states that we cannot use a dict that is not hashabale – but how do we do this?

How do we fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading