How I can convert a Python Tuple into Dictionary?

engrmahabub
Mahabubur Rahman
Published on Oct, 19 2023 1 min read 0 comments
image

Tuple elements are enclosed inside parentheses, while dictionary elements appear as a key-value pair and are enclosed in curly braces.

i.e- 

(
    (1, 'Pending'),
    (2, 'Done'),
    (3, 'Cancel')
)


In this article, we'll show you how to convert a Python tuple to a dictionary. We can use dict(tuple)

to convert tuple to dictionary.

Example - 

# input tuple
Tuple = ((1, 'Pending'),(2, 'Done'),(3, 'Cancel'))
    
print("The Tuple:",Tuple )    

Dictionary = dict(Tuple)   
print("The Dictionary:",Dictionary )  

Output -

The input Tuple: (1, 'Pending'),(2, 'Done'),(3, 'Cancel') 
The result dictionary: {1:'Pending',2:'Done',3:'Cancel')}  


 

0 Comments