Search code examples
pythonlistdictionarynestedlist-comprehension

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]

Solution

  • 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']]