A dictionary where values are lists can be inverted obtaining another dictionary where the keys are the elements in value lists of the original dictionary, and values are lists of the original keys.
For instance, if the original dictionary d is:
{ 'John' : ['AP1', 'CAOS', 'POC', 'MB'],
'Mary' : ['CAOS', 'MB', 'ALG'],
'Sheila' : ['CAL']
'Peter' : ['AP1', 'ALG', 'POC', 'CB', 'MB']
}
the function will return
{ 'AP1': ['John', 'Peter'],
'CAOS': ['John', 'Mary'],
'POC': ['John', 'Peter'],
'MB': ['John', 'Mary', 'Peter'],
'ALG': ['Mary', 'Peter'],
'CAL': ['Sheila']
'CB': ['Peter']
}
Input
The input is a dictionary in the following format:
Output
The output is the inverted dictionary. Keys are printed in lexicographical order, and so are their corresponding lists of values. Follow the format of the examples.
Input
John AP1 CAOS POC MB Mary CAOS MB ALG Sheila CAL Peter AP1 ALG POC CB MB
Output
ALG: Mary Peter AP1: John Peter CAL: Sheila CAOS: John Mary CB: Peter MB: John Mary Peter POC: John Peter
Input
carrots 3 4 5 potatoes 1 2 3 4 spinach 4 5 6 7 8 oranges 5 6 apples 2
Output
1: potatoes 2: apples potatoes 3: carrots potatoes 4: carrots potatoes spinach 5: carrots oranges spinach 6: oranges spinach 7: spinach 8: spinach
Input
john twitter insta github linkedin peter insta facebook mary twitter github facebook sheila insta github
Output
facebook: mary peter github: john mary sheila insta: john peter sheila linkedin: john twitter: john mary