Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove duplicate values in a dictionary?

I want to remove duplicate values inside a dictionary.

Let’s say this is my dictionary:

dict = {'X' : 
[
 {id : 1, Name: 'RandomName', Brand: 'RandomBrand'}, 
 {id: 2, Name: 'RandomName2', Brand: 'RandomBrand2'}
],
'Y': 
[
 {id: 1, Name: 'RandomName', Brand: 'RandomBrand'},
 {id: 1, Name: 'RandomName', Brand: 'RandomBrand'}
]
}

I want to have this as end-result after removing the duplicates

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

RESULT:

dict = {'X' : 
[
 {id : 1, Name: 'RandomName', Brand: 'RandomBrand'}, 
 {id: 2, Name: 'RandomName2', Brand: 'RandomBrand2'}
],
'Y': 
[
 {id: 1, Name: 'RandomName', Brand: 'RandomBrand'}
]
}

How would I do this?

>Solution :

Dictionaries by default don’t accept duplicates. Look closely, you have dict[str:list[dict[...]] there. So you want to filter duplicated dictionaries from list.

Answer:
If order doesn’t matter, and you want to remove only exact duplicates, just go this way:

structure = ... # your `dict` - you shouldn't use keyword as a variable name.
structure = {key: list(frozenset(tuple(d.items()) for d in value)) for key, value in st.items()}

If order matters, replace ‘list’ with ‘sorted’ and define key to sort it right.

@Edit: If performance is also a thing, you shouldn’t take that task yet, you’re lack of basics, and introducing proper structure is kind of overkill.

In case of any nested ‘list_comprehension’, I would highly discourage that, as it’ll most likely perform even more poorly than my example.

@Edit2:
Of course, if you want reverse it to proper structure, you can play with that further.

@Edit3:
example execution

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading