I have a list of dictionaries sorted by a specific key. Each dictionary contains 32 elements, and there over 4000 dictionaries in the list. I need code to work through the list and return a new list with all duplicates removed.
The methods from these links:
Don't help me because a dictionary is unhashable.
Any thoughts? If you need more info, comment and I will add the info.
Edit:
A duplicate dictionary would be any two dictionaries with the same values for list[dictionary][key]
.
Ok, here is a detailed explanation for those who need it.
I have a list of dictionaries like thus:
[ {
"ID" : "0001",
"Organization" : "SolarUSA",
"Matchcode" : "SolarUSA, Something Street, Somewhere State, Whatev Zip",
"Owner" : "Timothy Black",
}, {
"ID" : "0002",
"Organization" : "SolarUSA",
"Matchcode" : "SolarUSA, Something Street, Somewhere State, Whatev Zip",
"Owner" : "Johen Wilheim",
}, {
"ID" : "0003",
"Organization" : "Zapotec",
"Matchcode" : "Zapotec, Something Street, Somewhere State, Whatev Zip",
"Owner" : "Simeon Yurrigan",
} ]
Of this list, the first and second dictionaries are duplicates because their Matchcodes
are identical.
Now this list is sorted by the following code:
# sort_by is "Matchcode"
def sort( list_to_be_sorted, sort_by ):
return sorted(list_to_be_sorted, key=lambda k: k[sort_by])
So I have a neat list of dictionaries sorted by Matchcode
. Now I just need to iterate over the list, accessing the list[dictionary][key]
and deleting duplicates when two key values match.