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))

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.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program