简体   繁体   中英

Remove duplicates from the list of dictionaries

I have following list of dictionaries:

d = [
{ 'name': 'test', 'regions': [{'country': 'UK'}] },
{ 'name': 'test', 'regions': [{'country': 'US'}, {'country': 'DE'}] },
{ 'name': 'test 1', 'regions': [{'country': 'UK'}], 'clients': ['1', '2', '5'] },
{ 'name': 'test', 'regions': [{'country': 'UK'}] },
]

What is the easiest way to remove entries from the list that are duplicates ?

I saw solutions that work, but only if an item doesn't have nested dicts or lists

How about this:

new_d = []
for x in d:
    if x not in new_d:
        new_d.append(x)

For easiest as in "easy to implement", the one in this question seems pretty compact.

Not likely it's also very effective performance-wise though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM