How to solve TypeError: unhashable type 'list'

拜拜、爱过 提交于 2019-12-13 02:46:54

问题


I have two lists I would like to modify a particular content of the first list and send the output but when I try to modify it, am getting an error TypeError: unhashable type: 'list'. I am using python with mongodb.

The two lists are

data = [{"id" : ["5630baac3f32df134c18b682","564b22373f32df05fc905564"],"phone" : ["9988776655","9638527410"], "Request": "Support staff", "Date": "19-11-2015"}]
test = [{"phone" : "9638527410", "id": "5630baac3f32df134c18b682"}]
id = {}
for info in chain(data, test):
    id.setdefault(info['id'], {}).update(info)
res = list(id.values())
print(res)

The required output is

[{"phone" : "9638527410", "id": "5630baac3f32df134c18b682", "Request": "Support staff", "Date": "19-11-2015"}]

What is the mistake I am making here?

Can anyone guide me how I can get the desired output.


回答1:


You are using list as an key in the dictionary. List is not a hashable type in python. So, it can not be used as key in the dictionary. Only hashable types such as tuple, strings, numbers can be used as key in the dictionary.



来源:https://stackoverflow.com/questions/33772115/how-to-solve-typeerror-unhashable-type-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!