[Tutor] Remove duplicate values from dictionary without removing key
David Rock
david at graniteweb.com
Fri Jan 8 10:56:19 EST 2021
* Umar Draz <unix.co at gmail.com> [2021-01-08 14:44]:
> I want to replace duplicate values with empty "" in my dictionary. Here is
> my original dictionary.
>
> 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': ''}
> ]
I'm very curious about the end goal in your question. Are you sure you
want to remove the client value? Will that break a relationship between
items? This looks to me like what you actually want is a dictionary of
clients/names:
clients = {
'xyz': [
{'name': "Ilir Meta", 'rating': 0.06, 'total': 100},
{'name': "Abdelmadjid Tebboune", 'rating': 4.0, 'total': ''},
{'name': "Alexander Lukashenko", 'rating': 3.1, 'total': ''},
{'name': "Miguel Díaz-Canel", 'rating': 0.32, 'total': ''},
],
'udz': [
{'name': "Ilir Meta", 'rating': 0.06, 'total': 150},
{'name': "Abdelmadjid Tebboune", 'rating': 4.0, 'total': ''},
{'name': "Alexander Lukashenko", 'rating': 3.1, 'total': ''},
{'name': "Miguel Díaz-Canel", 'rating': 0.32, 'total': ''}
]
}
so rather than loop through and remove information, just add items to
the appropriate client list?
--
David Rock
david at graniteweb.com
More information about the Tutor
mailing list