Home > Back-end >  How to remove duplicate values in a dictionary?
How to remove duplicate values in a dictionary?

Time:05-10

I want to remove duplicate values inside a dictionary.

Let's say this is my dictionary:

dict = {'X' : 
[
 {id : 1, Name: 'RandomName', Brand: 'RandomBrand'}, 
 {id: 2, Name: 'RandomName2', Brand: 'RandomBrand2'}
],
'Y': 
[
 {id: 1, Name: 'RandomName', Brand: 'RandomBrand'},
 {id: 1, Name: 'RandomName', Brand: 'RandomBrand'}
]
}

I want to have this as end-result after removing the duplicates

RESULT:

dict = {'X' : 
[
 {id : 1, Name: 'RandomName', Brand: 'RandomBrand'}, 
 {id: 2, Name: 'RandomName2', Brand: 'RandomBrand2'}
],
'Y': 
[
 {id: 1, Name: 'RandomName', Brand: 'RandomBrand'}
]
}

How would I do this?

CodePudding user response:

Try with dictionary comprehension:

>>> {k: [dict(t) for t in {tuple(d.items()) for d in v}] for k,v in dct.items()}
{'X': [{'id': 2, 'Name': 'RandomName2', 'Brand': 'RandomBrand2'},
       {'id': 1, 'Name': 'RandomName', 'Brand': 'RandomBrand'}],
 'Y': [{'id': 1, 'Name': 'RandomName', 'Brand': 'RandomBrand'}]}
Input:
dct = {'X': [{"id": 1, "Name": 'RandomName', "Brand": 'RandomBrand'}, 
             {"id": 2, "Name": 'RandomName2', "Brand": 'RandomBrand2'}],
       'Y': [{"id": 1, "Name": 'RandomName', "Brand": 'RandomBrand'},
             {"id": 1, "Name": 'RandomName', "Brand": 'RandomBrand'}]}

CodePudding user response:

Dictionaries by default don't accept duplicates. Look closely, you have dict[str:list[dict[...]] there. So you want to filter duplicated dictionaries from list.

Answer: If order doesn't matter, and you want to remove only exact duplicates, just go this way:

structure = ... # your `dict` - you shouldn't use keyword as a variable name.
structure = {key: list(frozenset(tuple(d.items()) for d in value)) for key, value in st.items()}

If order matters, replace 'list' with 'sorted' and define key to sort it right.

@Edit: If performance is also a thing, you shouldn't take that task yet, you're lack of basics, and introducing proper structure is kind of overkill.

In case of any nested 'list_comprehension', I would highly discourage that, as it'll most likely perform even more poorly than my example.

@Edit2: Of course, if you want reverse it to proper structure, you can play with that further.

@Edit3: example execution

  • Related