Home > Net >  Remove duplicate from dictionary in python
Remove duplicate from dictionary in python

Time:11-10

I have 5 lists in a dictionary I want to delete duplicates and retain the element that occurs in first list by comparing all lists available

dict = {1:[0,1,2,3], 2:[1,4,5], 3:[0,4,2,5,6], 4:[0,2,7,8], 5:[9]}

Output should look like this:

dict = {1:[0,1,2,3], 2:[4,5], 3:[6], 4:[7,8], 5:[9]}

CodePudding user response:

You can make a set to store items that have been seen, and then sequentially update the dict according to the set:

d = {1:[0,1,2,3], 2:[1,4,5], 3:[0,4,2,5,6], 4:[0,2,7,8], 5:[9]}

seen = set()
for k, v in d.items():
    d[k] = [x for x in v if x not in seen]
    seen.update(d[k])

print(d) # {1: [0, 1, 2, 3], 2: [4, 5], 3: [6], 4: [7, 8], 5: [9]}

CodePudding user response:

A one-liner with a dictionary comprehension:

>>> {k: [i for i in v if i not in sum(list(dct.values())[:idx], [])] for idx, (k, v) in enumerate(dct.items())}
{1: [0, 1, 2, 3], 2: [4, 5], 3: [6], 4: [7, 8], 5: [9]}
>>> 

It filter and flattens all the values in the list before the certain key and filters the values not in there.

P.S. I renamed your dict to dct so it doesn't override the function name

  • Related