Remove duplicate dict in list in Python

前端 未结 12 926
太阳男子
太阳男子 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:46

    Not a universal answer, but if your list happens to be sorted by some key, like this:

    l=[{'a': {'b': 31}, 't': 1},
       {'a': {'b': 31}, 't': 1},
     {'a': {'b': 145}, 't': 2},
     {'a': {'b': 25231}, 't': 2},
     {'a': {'b': 25231}, 't': 2}, 
     {'a': {'b': 25231}, 't': 2}, 
     {'a': {'b': 112}, 't': 3}]
    

    then the solution is as simple as:

    import itertools
    result = [a[0] for a in itertools.groupby(l)]
    

    Result:

    [{'a': {'b': 31}, 't': 1},
    {'a': {'b': 145}, 't': 2},
    {'a': {'b': 25231}, 't': 2},
    {'a': {'b': 112}, 't': 3}]
    

    Works with nested dictionaries and (obviously) preserves order.

提交回复
热议问题