reverse keyword in python code example
Example 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)
Example 2: reverse python3
arr = [2,5,32,86,4,131,97]
for i in range(len(arr)-1, -1, -1):
print(arr[i])
arr.reverse()