Basic Programs

If Else Programs

While loop Programs

For loop Programs

List Programs

Dict Programs


Python program to Swap the Position of dictionary.


In this example shows, the dictionary is converted to equivalent key-value pairs in the form of tuples,next step the swap is performed and the tuple list is converted to the require format.

Example.py
values = {'Australia': '$', 'Egypt': '£', 'India': '', 'Japan': '¥', 'Nepal': ''}
i, j = 0, 4

# Swapping Values for the given positions... 
tuplt = list(values.items())
tuplt[i], tuplt[j] = tuplt[j], tuplt[i]
swapdict = dict(tuplt)

# Printing the dictionaries...
print("Dictionary = ", end = " ")
print(values)
print("After swapping = ", end = " ")
print(swapdict)

Output

Dictionary =  {'Australia': '$', 'Egypt': '£', 'India': '₹', 'Japan': '¥', 'Nepal': '₦'}
After swapping =  {'Nepal': '₦', 'Egypt': '£', 'India': '₹', 'Japan': '¥', 'Australia': '$'}

Prev Next