简体   繁体   中英

“Type Error: unhashable type: dict” python error

This is currently not finished but I'm trying to add a weapon to an “weapon_inventory” and whenever I try this error pops up. I'm a beginner to python so if you know how to fix this try explain it pretty simple so I can understand it and improve.

weapon_inventory = {}

weapon_found = "Boss Weapon"
weapon_inventory.update({weapon_found: {"name": weapon_found, "durability": 100}})
print("Inventory: ", ", ".join(weapon_inventory))

Your code snippet is working for me on Python 3.8.10. But to understand the error you said you are getting, you should learn how dictionaries work.

In Python, you can say there are two kinds of datatypes. Hashable and Non-Hashable. Hashable are those which are immutable (whose value doesn't change with time) like integer, string, tuple, etc. And non-hashable are those which are mutable (whose value might change with time) like lists, dicts, etc.

In a dictionary, your "Key" should be a hashable python datatype only. Means, you can't use lists, dicts, or sets, as your "Keys". If you try using a non-hashable datatype as your dict keys, then you'll get the error you have mentioned in the question title: TypeError: unhashable type: 'dict'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM