Remove duplicate dict in list in Python

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

    Not so short but easy to read:

    list_of_data = [{'a': 123}, {'b': 123}, {'a': 123}]
    
    list_of_data_uniq = []
    for data in list_of_data:
        if data not in list_of_data_uniq:
            list_of_data_uniq.append(data)
    

    Now, list list_of_data_uniq will have unique dicts.

提交回复
热议问题