Home > Enterprise >  How to remove duplicate dict from list in python
How to remove duplicate dict from list in python

Time:05-18

I have list of dicts where sometime there are duplicate dict. For ex:

objList = 
[{
    'Name': 'plate',
    'StartTime': '2022-05-17T10:26:05.738101',

}, {
    'Name': 'bezel',
    'StartTime': '2022-05-17T10:26:09.922667',

}, {
    'Name': 'chrome',
    'StartTime': '2022-05-17T10:26:23.283304',

}, {
    'Name': 'plate placement',
    'StartTime': '2022-05-17T10:26:25.570845',

}, {
    'Name': 'plate placement',
    'StartTime': '2022-05-17T10:26:39.3390',
}]

In above data, plate placement is duplicated. Similarly, any dict can be duplicated but I have delete any of the duplicate data and just keep one. For this, first I thought of checking if in the list we have duplicate dicts or not:

obj_names = []
for obj in objList:
    obj_names.append(obj['Name'])

Now obj_names contains ['plate', 'bezel', 'chrome', 'plate placement', 'plate placement']. So this way we know that which dict is duplicated. We now have to delete any one of its occurrences. But I am not sure how we can delete that occurrence from the list. Can anyone please suggest something? Thanks

CodePudding user response:

As @mugiseyebrows said, we use the 'Name' of each dictionary (this statement is not very rigorous.) as the key and the dictionary itself as the value to create a new dictionary so that you can ensure that a dictionary with the same 'Name' appears once, and then use its values to create a new list:

>>> new_dict = {dct['Name']: dct for dct in objList}
>>> new_list = list(new_dict.values())
>>> print('},\n'.join(str(new_list).split('},')))
[{'Name': 'plate', 'StartTime': '2022-05-17T10:26:05.738101'},
 {'Name': 'bezel', 'StartTime': '2022-05-17T10:26:09.922667'},
 {'Name': 'chrome', 'StartTime': '2022-05-17T10:26:23.283304'},
 {'Name': 'plate placement', 'StartTime': '2022-05-17T10:26:39.3390'}]

CodePudding user response:

Instead of list of dictionaries you can use dictionary of dictionaries using name as a key. This way if you insert new object with same name it will replace previous object.

If you need to preserve order you can use OrderedDict.

  • Related