pablox
pablox

Reputation: 641

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

Upvotes: 10

Views: 14604

Answers (1)

wim
wim

Reputation: 362577

How about this:

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

Upvotes: 25

Related Questions