[Tutor] Remove duplicate values from dictionary without removing key

Umar Draz unix.co at gmail.com
Fri Jan 8 09:03:50 EST 2021


Hi Alan

The function you gave me worked but it has a little issue, here is the code

from pprint import pprint


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


items = [

    {'client': 'xyz', 'name': "Ilir Meta", 'rating': 0.06, 'total': 100},

    {'client': 'xyz','name': "Abdelmadjid Tebboune", 'rating': 4.0,
'total': 100},

    {'client': 'xyz','name': "Alexander Lukashenko", 'rating': 3.1,
'total': 100},

    {'client': 'xyz', 'name': "Miguel Díaz-Canel", 'rating': 0.32, 'total':
100},

    {'client': 'udz', 'name': "Ilir Meta", 'rating': 0.06, 'total': 150},

    {'client': 'udz','name': "Abdelmadjid Tebboune", 'rating': 4.0,
'total': 150},

    {'client': 'udz','name': "Alexander Lukashenko", 'rating': 3.1,
'total': 150},

    {'client': 'udz', 'name': "Miguel Díaz-Canel", 'rating': 0.32, 'total':
150},

]


items = remove_duplicate_values(items)


pprint(items)



Here is the output


[{'client': 'xyz', 'name': 'Ilir Meta', 'rating': 0.06, 'total': 100},

 {'client': '', 'name': 'Abdelmadjid Tebboune', 'rating': 4.0, 'total': ''},

 {'client': '', 'name': 'Alexander Lukashenko', 'rating': 3.1, 'total': ''},

 {'client': '', 'name': 'Miguel Díaz-Canel', 'rating': 0.32, 'total': ''},

 {'client': 'udz', 'name': '', 'rating': '', 'total': '150'},

 {'client': '', 'name': '', 'rating': '', 'total': ''},

 {'client': '', 'name': '', 'rating': '', 'total': ''},

 {'client': '', 'name': '', 'rating': '', 'total': ''}]



You can see the name and rating values also marked empty for client (udz),
now is it possible that the function only check client key



On Fri, Jan 8, 2021 at 6:51 PM Alan Gauld via Tutor <tutor at python.org>
wrote:

> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


-- 
Umar Draz


More information about the Tutor mailing list