Remove duplicate dict in list in Python

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

    Other answers would not work if you're operating on nested dictionaries such as deserialized JSON objects. For this case you could use:

    import json
    set_of_jsons = {json.dumps(d, sort_keys=True) for d in X}
    X = [json.loads(t) for t in set_of_jsons]
    

提交回复
热议问题