1# Python3 using reversed() + items()
2test_dict = {'Hello' : 4, 'to' : 2, 'you' : 5}
3
4rev_dict = dict(reversed(list(test_dict.items()))) # Reversing the dictionary
5
6print("The original dictionary : " + str(test_dict))
7print("The reversed order dictionary : " + str(rev_dict))
8
9# Output:
10"The original dictionary : {'Hello': 4, 'to': 2, 'you': 5}"
11"The reversed order dictionary : {'you': 5, 'to': 2, 'Hello': 4}"
1def inverse_dict(my_dict):
2 """
3 the func get a dictinary and reverse it, the keys become values and the values become keys.
4 :param my_dict: the dictinary that need to be reversed.
5 :return: a VERY pretty dictionary.
6 """
7 result_dict = {}
8 for key, value in my_dict.items():
9 if not value in result_dict.keys():
10 result_dict[value] = []
11 result_dict[value].append(key)
12 return result_dict, print(result_dict)