0
How to append to a list which is a value in dictionary?(TypeError: unhashable type 'list')
I have a list as my value for a key in my dictionary. When i try to append a variable to that list by using the below code it's throwing me an type error. My code is this: my_list=my_dict.get(key) my_list.append(value) my_dict.update({key,my_list}) But throws me an type error as follows: TypeError : unhashable type 'list' Is there any way to solve this?
3 ответов
+ 1
TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed. The standard way to solve this issue is to cast a list to a tuple.
http://net-informations.com/JUMP_LINK__&&__python__&&__JUMP_LINK/iq/unhashable.htm
+ 1
Since lists are mutable they can't be used as dictionary keys. You can try to use tuples instead. And when you want to change that key, remove the value using the old key then insert using the new key.
0
or he could throw it into a for loop to loop through the list and append everything in that list to the dictionary but that'd be doin a bit much