Write a function called "reverse" that accepts a dictionary as a parameter and returns a dictionary that is the reverse of the original. Note: The keys and values of the dictionary are both strings {string: string}. In addition, the reverse of the dictionary is a
Write a function called "reverse" that accepts a dictionary as a parameter and returns a dictionary that is the reverse of the original. Note: The keys and values of the dictionary are both strings {string: string}. In addition, the reverse of the dictionary is a new dictionary that uses the values of the original dictionary as its keys and the original keys now as the values. Since the dictionary's keys must be unique but the values do not need to be, you should have the values map to a set of keys. For example, if passed the following dictionary:
{31 : "Tim", 19 : "Ben", 57 : "Sarah", 14 : "Ben", 21 : "Hannah", 5 : "Tim", 42 : "Ben"}
You should reverse it to become (the order of the keys and values does not matter):
{ "Tim" : [31, 5] , "Ben" : [19, 14, 42], "Sarah": [57], "Hannah": [21] }
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images