0

I have a dictionary with duplicate values for one key. How could I remove the duplicates?

Initial dictionary:

dict = {"a": [1, 1, 1, 2, 1, 3, 2, 3, 1], "b": [4, 5, 2, 1, 1, 5, 6, 2, 5]}

Desired dictionary:

dict = {"a": [1, 2, 3], "b": [4, 5, 2, 1, 6]}
dmigo
  • 2,849
  • 4
  • 41
  • 62
adaresa
  • 13
  • 4

3 Answers3

4

Use dictionary comprehension to build a new dictionary, eliminating duplicates through the use of set:

dict = {"a": [1, 1, 1, 2, 1, 3, 2, 3, 1], "b": [4, 5, 2, 1, 1, 5, 6, 2, 5]}
dict = { key : list(set(value)) for key, value in dict.items()}

Converting a list to a set removes duplicates. We then convert it back to a list. Note that it doesn't necessarily preserve the original order of items in each list.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
0

1- Solution 1: If you have control over the data while its being constructed, then store the data in a set instead as a list

2- Solution 2: If data is coming from api or database then you can process the data in the following way:

dictionary = {"a": [1, 1, 1, 2, 1, 3, 2, 3, 1], "b": [4, 5, 2, 1, 1, 5, 6, 2, 5]}
for each_key, value in dictionary.items():
   dictionary[each_key] = list(set(value))

Note: You lose the order of your lists. Also, you should not keep your variable name as dict as its a python keyword

Archit Dwivedi
  • 416
  • 4
  • 9
0

You should use set instead of list internally.

If list is the only thing you want to use then do this,

myDict = dict({"a": [1, 1, 1, 2, 1, 3, 2, 3, 1], "b": [4, 5, 2, 1, 1, 5, 6, 2, 5]})

for key,listOfDup in myDict.items():
    myDict[key] = list(set(listOfDup))
Mandar Autade
  • 336
  • 2
  • 12