Answers for "Write a function to invert a dictionary. It should accept a dictionary as a parameter and return a dictionary where the keys are values from the input dictionary and the values are lists of keys from the input dictionary."

5

invert dictionary python

inv_map = {v: k for k, v in my_map.items()}
Posted by: Guest on August-23-2020
1

how to reverse a dictionary in python

def inverse_dict(my_dict):
    """
    the func get a dictinary and reverse it, the keys become values and the values become keys.
    :param my_dict: the dictinary that need to be reversed.
    :return: a VERY pretty dictionary.
    """
    result_dict = {}
    for key, value in my_dict.items():
        if not value in result_dict.keys():
            result_dict[value] = []
        result_dict[value].append(key)
    return result_dict, print(result_dict)
Posted by: Guest on May-17-2020

Code answers related to "Write a function to invert a dictionary. It should accept a dictionary as a parameter and return a dictionary where the keys are values from the input dictionary and the values are lists of keys from the input dictionary."

Python Answers by Framework

Browse Popular Code Answers by Language