[Tutor] Remove duplicate values from dictionary without removing key
Alan Gauld
alan.gauld at yahoo.co.uk
Fri Jan 8 08:50:56 EST 2021
On 08/01/2021 13:25, Alan Gauld via Tutor wrote:
> OK, Lets take it one bit at a time.
To summarize and make it easier to use in your existing code I;ll turn
it into a function:
def remove_duplicate_values(dict_list):
seen = {}
for key in dict_list[0].keys():
seen[key] = []
for dic in dict_list:
for field,value in dic.items():
if value in seen[field]:
dic[field] = ""
else: seen[field].append(value)
return dict_list
Call it with:
items = remove_duplicate_values(items)
That function should work for any list of similar dictionaries.
But again untested!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list