Write a function that inverts a dictionary with non-unique values. Keys that map to the same values should be appended to a list in the inverted dictionary
Topic: Write a function that inverts a dictionary with non-unique values. Keys that map to the same values should be appended to a list in the inverted dictionary
Solution
def invert_dict_non_unique(my_dict): my_inverted_dict = dict() for key, value in my_dict.items(): my_inverted_dict.setdefault(value, list()).append(key) return my_inverted_dict
List all Python Programs