This code will switch keys to values and vice versa in dictionary.Code wont work as required if dictionary have same values or values are other then Number or String.
For example:
Dict = { ‘a’ : 1, ‘b’ : ‘2’, ‘c’ : 3}
Python 3.6:
def SwapKeyVal (x):
SwapedDict = {value:key for key, value in x.items()}
print (SwapedDict)
Dict = {‘a’:1,’b’:2,’c’:3}
SwapKeyVal(Dict)
Output:
>>> {1: ‘a’, 2: ‘b’, 3: ‘c’}
Python 2.7:
def SwapKeyVal (x):
SwapedDict = x((value,key) for key, value in x.iteritems())
print SwapedDict
Dict = {‘a’:1,’b’:2,’c’:3}
SwapKeyVal(Dict)
Output:
>>> {1: ‘a’, 2: ‘b’, 3: ‘c’}