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

Find duplicate values in list of dictionaries

I need to find dictionaries in a list that have the same key-value, and create a new list in which only the first dictionary is kept.

Example list:

lst_in = [{'First': 1, 'Second': 4}, {'First': 2, 'Second': 5}, {'First': 3, 'Second': 4}]

Duplicate key-value to iterate over should be ‘Second’. So in the example the first and third dictionary are the same.
I have tried looking at Find duplicates in python list of dictionaries and python list of dictionaries find duplicates based on value, but I can’t find the exact answer. I am only looking at one key-value. The dictionaries will always have the same keys.

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

Expected output:

lst_out = [{'First': 1, 'Second': 4}, {'First': 2, 'Second': 5}]

>Solution :

Sets are great for those "have I already seen this?" problems.

lst_in = [{'First': 1, 'Second': 4}, {'First': 2, 'Second': 5}, {'First': 3, 'Second': 4}]

found = set()
lst_out = []
for dct in lst_in:
    if dct['Second'] not in found:
        lst_out.append(dct)
        found.add( dct['Second'] )
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