Create a dictionary from tuples
Here we will create a dictionary from nested tuples and for that we need to pass two values in each tuple one will represent key and the other its corresponding value in the dictionary.
Syntax:
dict((value, key) for key,value in nested_tuple)
Example: Create a dictionary from tuples
Python3
# one value is age of student # second value is student name data = (( 24 , "bobby" ), ( 21 , "ojsawi" )) # convert into dictionary final = dict ((value, key) for key, value in data) # display print (final) |
Output:
{'bobby': 24, 'ojsawi': 21}
Python – Create Dictionary Of Tuples
In this article, we will discuss how to create a dictionary of tuples in Python.