假设我有一个字典列表:

[
    {'id': 1, 'name': 'john', 'age': 34},
    {'id': 1, 'name': 'john', 'age': 34},
    {'id': 2, 'name': 'hanna', 'age': 30},
]

如何获得唯一字典的列表(删除重复项)?

[
    {'id': 1, 'name': 'john', 'age': 34},
    {'id': 2, 'name': 'hanna', 'age': 30},
]

当前回答

如果字典中没有唯一的id,那么我将保持简单,并定义如下函数:

def unique(sequence):
    result = []
    for item in sequence:
        if item not in result:
            result.append(item)
    return result

这种方法的优点是,您可以为任何可比较的对象重用此函数。它使您的代码非常可读,适用于所有现代版本的Python,保持字典中的顺序,并且与其他选项相比速度也很快。

>>> L = [
... {'id': 1, 'name': 'john', 'age': 34},
... {'id': 1, 'name': 'john', 'age': 34},
... {'id': 2, 'name': 'hanna', 'age': 30},
... ] 
>>> unique(L)
[{'id': 1, 'name': 'john', 'age': 34}, {'id': 2, 'name': 'hanna', 'age': 30}]

其他回答

由于id足以检测重复项,且id是可哈希的:在以id为键的字典中运行它们。每个键的值都是原始字典。

deduped_dicts = dict((item["id"], item) for item in list_of_dicts).values()

在Python 3中,values()不返回列表;你需要在list()中包装整个表达式的右边,并且你可以将表达式的部分更经济地写成dict理解:

deduped_dicts = list({item["id"]: item for item in list_of_dicts}.values())

注意,结果可能与原始结果的顺序不同。如果这是一个要求,您可以使用集合。OrderedDict而不是dict。

顺便说一句,将数据保存在使用id作为键的字典中可能很有意义。

因此,创建一个临时字典,键为id。这将过滤掉重复的内容。 dict的values()将是列表

在Python2.7

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ]
>>> {v['id']:v for v in L}.values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

在Python3

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ] 
>>> list({v['id']:v for v in L}.values())
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

在Python2.5/2.6

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ] 
>>> dict((v['id'],v) for v in L).values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

这里提到的所有答案都很好,但在一些答案中,如果字典项有嵌套的列表或字典,就会面临错误,所以我建议简单的答案

a = [str(i) for i in a]
a = list(set(a))
a = [eval(i) for i in a]

在python 3中,简单的技巧,但基于唯一字段(id):

data = [ {'id': 1}, {'id': 1}]

list({ item['id'] : item for item in data}.values())

一个快速的解决方案是生成一个新列表。

sortedlist = []

for item in listwhichneedssorting:
    if item not in sortedlist:
        sortedlist.append(item)