-1

I have a dictionary:

d = {'Trump': ['MAGA', 'FollowTheMoney'],
     'Clinton': ['dems', 'Clinton'],
     'Stein': ['FollowTheMoney', 'Atlanta']}

I want to remove the duplicate strings in the list of strings which is a value for the key.

For this example, the desired result is

update_d = {'Trump': ['MAGA'],
            'Clinton': ['dems', 'Clinton'],
            'Stein': ['Atlanta']}

There was a similar question asked here, but I haven't been able to modify it for my purposes.

My Attempt:

new_d = {}
for key in d:  
    for key2 in d:
        lst = d[key]
        lst2 = d[key2]

        for string in lst:  
           for string2 in lst2:
              if string not in new_d:

My problem is that I want to compare the values of all the keys and remove the duplicates. But, I don't see how this can be achieved

Community
  • 1
  • 1
GRAp
  • 21
  • 1
  • 1
  • 3

2 Answers2

1

You can use Counter to tally how many times each value appears in d.

d = {'Trump': ['MAGA', 'FollowTheMoney'],
     'Clinton': ['dems', 'Clinton'],
     'Stein': ['FollowTheMoney', 'Atlanta']}

from collections import Counter

c = Counter(x for xs in d.values() for x in xs)

In this example, the value of c is

Counter({'Atlanta': 1,
         'Clinton': 1,
         'FollowTheMoney': 2,
         'MAGA': 1,
         'dems': 1})

Then choose values for which the count is exactly 1.

update_d = {k: [v for v in vs if c[v] == 1] for k, vs in d.items()}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • I condensed down the dictionary considerably, so the above example would't work. Also I cant use any modules for this assignment. I tried using a for loop to iterate over the keys but I can't seem to be able to compare different values of the same dictionary using a nested for loop. – GRAp Dec 05 '16 at 23:42
  • @GRAp if you can't use `Counter` in your assignment, I suggest you think about how `Counter` is implemented and then implement (the counting part) yourself. It is not difficult. – Joel Cornett Dec 06 '16 at 00:11
  • So, for each occurrence of a string I increment a new variable say, i by 1. (i +=1). But I'm trying to retain the relationship between the value and the key, and I don't know how to go about it. – GRAp Dec 06 '16 at 00:23
  • Can you edit the question to include a second example that demonstrates why this answer doesn't work in general? – Chris Martin Dec 06 '16 at 00:40
1

Not as elegant as using Counter, but does remove duplicates without the use of modules:

d = {'Trump': ['MAGA', 'FollowTheMoney'],
    'Clinton': ['dems', 'Clinton'],
    'Stein': ['FollowTheMoney', 'Atlanta']}

dupvals = [item for sublist in d.values() for item in sublist] # get all values from all keys into a list
dups = [] # list to hold duplicates

for i in dupvals:
    if dupvals.count(i) > 1:
        dups.append(i)

dupvals = set(dups) # keep only one item for each duplicated item

new_d = {}

for key,values in d.items():
    for value in values:
        if not value in dupvals:
            new_d.setdefault(key, []).append(value)

print new_d # {'Clinton': ['dems', 'Clinton'], 'Trump': ['MAGA'], 'Stein': ['Atlanta']}
  • THANK YOU. It works perfectly. Just curious what does the variable 'dupvals' do exactly. – GRAp Dec 06 '16 at 01:09
  • Glad to be of help. Sorry I should have explained more with comments. I used `dupvals` as a `list` variable to represent "any duplicate values in all of the values". It get all the values from all of the keys. Then iterate through the `dupvals` list to find all items that occur more than once. – chickity china chinese chicken Dec 06 '16 at 01:14
  • Ok. See, I wish I could come up with something like this. Once again thank you my solution wasn't working and was really incoherent. – GRAp Dec 06 '16 at 01:18
  • No worries, as you can see my solution is quite incoherent as well and not as elegant as it could be, but it got the job done :) Glad you could make sense of it. – chickity china chinese chicken Dec 06 '16 at 01:21