Write a Python program to convert a tuple to a dictionary


The program is written in Python and it declares a tuple named t with four elements, each of which is a tuple containing two values. The t tuple represents key-value pairs.

The program then creates a dictionary from the t tuple using a dictionary comprehension. The dictionary comprehension iterates over each element in the t tuple and creates key-value pairs. The first value of each tuple element becomes the key and the second value becomes the value. Finally, the created dictionary is passed as an argument to the print function and the dictionary is printed on the console.

Source Code

#create a tuple
t = (("Name", "Ram"),("Age", 23),("City", "Salem"),("Mark", 422))
print(dict((k,v) for k, v in t))

Output

{'Name': 'Ram', 'Age': 23, 'City': 'Salem', 'Mark': 422}


Example Programs