0

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. How can we delete that occurrence from the list?

1
  • @MechanicPig Can you please explain this in an answer? Commented May 17, 2022 at 11:01

2 Answers 2

1

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'}]
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.