In [1]:
d = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

In [2]:
d_swap = {v: k for k, v in d.items()}
print(d_swap)


{'val1': 'key1', 'val2': 'key2', 'val3': 'key3'}

In [3]:
def get_swap_dict(d):
    return {v: k for k, v in d.items()}

In [4]:
d_swap = get_swap_dict(d)
print(d_swap)


{'val1': 'key1', 'val2': 'key2', 'val3': 'key3'}

In [5]:
d_duplicate = {'key1': 'val1', 'key2': 'val1', 'key3': 'val3'}

In [6]:
d_duplicate_swap = get_swap_dict(d_duplicate)
print(d_duplicate_swap)


{'val1': 'key2', 'val3': 'key3'}