w3resource logo
Python Exercises

Python Exercise: Convert a tuple to a dictionary

Python tuple: Exercise-16 with Solution

Write a Python program to convert a tuple to a dictionary.

Sample Solution:-

Python Code:

>>> #create a tuple
>>> tuplex = ((2, "w"),(3, "r"))
>>> print(dict((y, x) for x, y in tuplex))

Sample Output:

{'r': 3, 'w': 2} 

Python Code Editor:

#create a tuple
tuplex = ((2, "w"),(3, "r"))
print(dict((y, x) for x, y in tuplex))

Improve this sample solution and post your code through Disqus