[Tutor] Remove duplicate values from dictionary without removing key

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jan 8 08:25:52 EST 2021


Please always use Reply All or reply List when reponding to list emails,
otherwise
they only go to the individual person who posted. I've CCd the list on
this reply.


On 08/01/2021 12:24, Umar Draz wrote:
> I am totally new in Python, so if there is any example then it will be
> helpful for me,
>
OK, Lets take it one bit at a time.

In your case you want in pseudo code:


seen = { "client":[], "name:[], "rating":[], "total": [] }

>
>     for each dictionary in the list
>
for dic in items:

>         for each field in the dictionary
>
     for field,value in dic.items():

>            check if the value has already been seen
>
            if value in seen[field]:

>            if so replace the value with ""
>
                 dic[field] = ""

>            else store the value for future reference but do not change it.
>
            else: seen[field].append(value)


And that should work, although I haven't tested it so there may be some
minor errors,
which I'll leave as an exercise! :-).

Try it and if you can't get it to work come back with a cut n paste of
your exact
code plus any error messages you get (in full)

-- 

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