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?

25th Dec 2016, 8:31 AM
Pavan Teja
Pavan Teja - avatar
3 Antworten
+ 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
11th Feb 2019, 5:13 AM
rahul kumar
rahul kumar - avatar
+ 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.
26th Dec 2016, 8:41 AM
Igor B
Igor B - avatar
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
31st Dec 2016, 6:25 AM
Eric Reed
Eric Reed - avatar