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.
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'] )