Oct-27-2020, 03:24 PM
Hello,
The input is this:
My code is this:
The input is this:
Output:{'Accurate': ['exact', 'precise'], 'exact': ['precise'], 'astute': ['Smart', 'clever'], 'smart': ['clever', 'bright', 'talented']}
And the output is supposed to be this:Output:{'precise': ['accurate', 'exact'], 'clever': ['astute', 'smart'], 'talented': ['smart'], 'bright': ['smart'], 'exact': ['accurate'], 'smart': ['astute']}
Note that values of each key are sorted.My code is this:
def reverse_dictionary(input_dict): my_dict= {} for key, value in input_dict.items(): for string in value: my_dict.setdefault(string.lower(), []).append(key.lower()) output_dict={k:v for k,v in sorted(my_dict.items(), key=lambda item:item[1])} return output_dictBut this code is not producing the expected result. What am I missing here?