Rosscoasks
Rosscoasks

Reputation: 107

Get keys from a dictionary that contains duplicate list values, then store their corresponding keys in a nested list

I've tried to word this the best way that I possibly can, but it will probably be more clear if I provide an example of what I am trying to acheive:

Input:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

Intended output:

[["person1","person2"],["person3","person4","person5"]]

Handling the lists in the dictionary is proving to be quite a challenge.

Appologies, I forgot to include what I have tried so far. As mentioned above - I am having issues with the lists:

rev_dict = {}
  
for key, value in source_dictionary.items():
    rev_dict.setdefault(value, set()).add(key)
      
result = [key for key, values in rev_dict.items()
                              if len(values) > 1]

Upvotes: 1

Views: 63

Answers (2)

mozway
mozway

Reputation: 260735

Assuming you want to join the keys by identical value, use a defaultdict:

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

from collections import defaultdict

d = defaultdict(list)
for key, value in source_dictionary.items():
    d[tuple(value)].append(key)
    
out = list(d.values())

Alternative with setdefault:

d = {}
for key, value in source_dictionary.items():
    d.setdefault(tuple(value), []).append(key)
    
out = list(d.values())

output:

[['person1', 'person2'], ['person3', 'person4', 'person5']]

Upvotes: 4

Ayyoub ESSADEQ
Ayyoub ESSADEQ

Reputation: 788

source_dictionary = {"person1": ["x1","x2","x3","x4"],
                     "person2": ["x1","x2","x3","x4"],
                     "person3": ["x1","x2"],
                     "person4": ["x1","x2"],
                     "person5": ["x1","x2"]
                    }

L = []
for i in source_dictionary.values():
    K = []
    for j in source_dictionary.keys():
        if source_dictionary[j] == i :
            K.append(j)
    if K not in L:
        L.append(K)

print(L)

Upvotes: 0

Related Questions