I want to find nonunique values in the original dictionary and store the keys associated with the value in a list. then store the nonunique value as a key and the list of keys as a value in a new dictionary.
Example:
>>> invert({'one':1, 'two':2, 'three':3, 'four':4}) #Input
{1: 'one', 2: 'two', 3: 'three', 4: 'four'} #output
>>> invert({'one':1, 'two':2, 'uno':1, 'dos':2, 'three':3}) #input
{1: ['one', 'uno'], 2: ['two', 'dos'], 3: 'three'} #output
>>> invert({'123-456-78':'Sara', '987-12-585':'Alex', '258715':'sara', '00000':'Alex'})
{'Sara': '123-456-78', 'Alex': ['987-12-585', '00000'], 'sara': '258715'}
def invert(d):
pass
inverted_dict = {}
non_unique_lst = []
for key,value in d.items() :
if value in inverted_dict:
non_unique_lst = [key]
inverted_dict[value,non_unique_lst]
print (non_unique_lst)
else:
inverted_dict[value] = key
print (inverted_dict)
return inverted_dict