Write a function that accepts a dictionary as an argument. If the dictionary contains duplicate values, it should return an empty dictionary. Otherwise, it should return a new dictionary where the values become the keys and the keys become the values.

Source Code

def swap_key_value(d):
    L = list(d.values())
    for value in L:
        if L.count(value) > 1:
            return dict()

    new_dict = {}
    for k, v in d.items():
        new_dict[v] = k
    return new_dict


d = {'a':10,'b':20,'c':20}
print(d)
n = swap_key_value(d)
print(n)

d = {'a':10,'b':20,'c':30}
print(d)
n = swap_key_value(d)
print(n)

Output

{'a': 10, 'b': 20, 'c': 20}
{}
{'a': 10, 'b': 20, 'c': 30}
{10: 'a', 20: 'b', 30: 'c'}