I want to create a new dictionary (new_dict), from the old dictionary(my_dict), in new_dict values of the my_dict will be keys and keys will be values.
Expected output:
{"name" : [1, 3, 5], "last_name": [2, 4, 6]} But, I am getting:
{"name" : [1, 2, 3, 4, 5, 6], "last_name": [1, 2, 3, 4, 5, 6]} This is my code:
def my_function(my_dict): # Creating new dictionary new_dict = dict.fromkeys(set(my_dict.values()), []) for key, value in my_dict.items(): new_dict[value].append(key) return new_dict # Existing dictionary my_dict = { 1: "name", 2: "last_name", 3: "name", 4: "last_name", 5: "name", 6: "last_name"} res = my_function(my_dict) print(res)