Remove duplicate dict in list in Python

前端 未结 12 917
太阳男子
太阳男子 2020-11-22 09:10

I have a list of dicts, and I\'d like to remove the dicts with identical key and value pairs.

For this list: [{\'a\': 123}, {\'b\': 123}, {\'a\': 123}]<

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 09:47

    Sometimes old-style loops are still useful. This code is little longer than jcollado's, but very easy to read:

    a = [{'a': 123}, {'b': 123}, {'a': 123}]
    b = []
    for i in range(0, len(a)):
        if a[i] not in a[i+1:]:
            b.append(a[i])
    

提交回复
热议问题