```test = [{"a": 1}, {"a": 1}, {"a": 3}, {"b": 4}]
test = list(set(test))
>>>TypeError: unhashable type: 'dict'```

## 1.使用reduce方法

reduce() 函数会对参数序列中元素进行累积。

```from functools import reduce
>>>def add(x, y) :            # 两数相加
...    return x + y
...
15```

```from functools import reduce
>>> reduce(lambda x, y: x+y, [1,2,3,4,5])  # 使用 lambda 匿名函数
15```

```from functools import reduce

data = [{"a": 1}, {"a": 1}, {"a": 3}, {"b": 4}]
result = []
def unduplicate(result, data):
if data not in result:
result = result + [data]
return result

for i in data:
result = unduplicate(result, i)

>>> result
>>> [{'a': 1}, {'a': 3}, {'b': 4}]```

```def delete_duplicate(data):
func = lambda x, y: x + [y] if y not in x else x
data = reduce(func, [[], ] + data)
return data

>>> delete_duplicate(data)
>>> [{'a': 1}, {'a': 3}, {'b': 4}]```

`data = reduce(lambda x, y: x + [y] if y not in x else x, [[], ] + data)`

## 2.奇怪的技巧

```data = [{"a": 1}, {"a": 1}, {"a": 3}, {"b": 4}]
def delete_duplicate(data):
immutable_dict = set([str(item) for item in data])
data = [eval(i) for i in immutable_dict]
return data
>>> delete_duplicate(data)
>>> [{'a': 1}, {'a': 3}, {'b': 4}]```

1.遍历字典，将每个子项变成字符串存放到数组中，再通过set函数去重。

2.通过eval函数，将去重后的数组里的每个子项重新转化回字典。

## 3.高效的方式

```data = [dict(t) for t in set([tuple(d.items()) for d in data])]
>>>data
>>>[{'a': 1}, {'b': 2}]```

`data2 = [{"a": {"b": "c"}}, {"a": {"b": "c"}}]`

```data2 = [{"a": {"b": "c"}}, {"a": {"b": "c"}}]
def delete_duplicate_str(data):
immutable_dict = set([str(item) for item in data])
data = [eval(i) for i in immutable_dict]
return data
print(delete_duplicate_str(data2))

>>> [{'a': {'b': 'c'}}]```

​Python实用宝典 ( pythondict.com )