0

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]}
5
  • 1
    what have you tried so far? Commented Nov 10, 2021 at 6:43
  • Also not that there is no actual order to the keys/values in a dictionary. What happens if I look at 2 before I look at 1. Do you require the keys to be sorted numerically? Commented Nov 10, 2021 at 6:51
  • @sahasrara62 I Tried running a loop over the dictionary, stored them in two separate variables and compared them using a set operation Commented Nov 10, 2021 at 6:52
  • @FrankYellin From python 3.7, dict does preserve order. Commented Nov 10, 2021 at 6:53
  • @j1-lee. You are correct. In 3.6, this was an implementation detail of CPython. But I see that in 3.7, it's been made official. Thanks for the correction. Commented Nov 10, 2021 at 16:53

2 Answers 2

2

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]}
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Nice, not even just quadratic but cubic runtime. Can you make it quartic? :-P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.