Remove duplicate values from dictionary without removing key
MRAB
python at mrabarnett.plus.com
Fri Jan 8 15:03:43 EST 2021
On 2021-01-08 09:56, Umar Draz wrote:
> I want to replace duplicate values with empty "" in my dictionary. Here is
> my original dictionary.
>
> 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': 100},
> {'client': 'udz', 'name': "Alexander Lukashenko", 'rating': 3.1,
> 'total': 150},
> {'client': 'udz', 'name': "Miguel Díaz-Canel", 'rating': 0.32, 'total': 150}
> ]
>
>
> Now I want to create another dict with like this
>
> items = [
> {'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': "Ilir Meta", 'rating': 0.06, 'total': 150},
> {'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': ''}
> ]
>
>
> Would you please help me how I can do this? and if this is not the right
> place to ask this question then please help me where I can ask this? I had
> tried stackoverflow but that not worked.
>
Iterate through the list of dicts.
If you've seen the client before, then set the appropriate value or
values in the dict to ''.
You can use a set to remember which clients you've already seen.
More information about the Python-list
mailing list