Python program to Convert a list of Tuples into Dictionary














































Python program to Convert a list of Tuples into Dictionary



Description:
This is a Basic program to convert a list of Tuples into Dictionary. It works in 3 steps:
1. Creating a class 
2. Creating a function inside the class
3. Calling the function from main with object and argument.
The function "tupleToDict" is used to convert a list of Tuples into Dictionary by using the Type conversion or type casting.
In this, first the list of tuple is formed by entering tuples one by one and then it is typecasted to dictionary.
Program:
#Python program to Convert a list of Tuples into Dictionary class convert(): def __init__(self, arg): self.arg = arg def tupleToDict(self): return dict(self.arg) if __name__=='__main__': lst=[] n=int(input('Enter the number of tuples in the list = ')) print('Enter elements of the list:-') # combinig tuples and forming list. for i in range(n): print('tuple ',(i+1),' :-') temp = [] temp.append(input('Key = ')) temp.append(int(input('Value = '))) nt = tuple(temp) lst.append(nt) print('The entered list is = ',lst) obj = convert(lst) new = obj.tupleToDict() print('The resulting dictionary is = ',new)
Output:
Enter the number of tuples in the list = 4 Enter elements of the list:- tuple 1 :- Key = hi Value = 1 tuple 2 :- Key = hello Value = 2 tuple 3 :- Key = yeh Value = 4 tuple 4 :- Key = no Value = 6 The entered list is = [('hi', 1), ('hello', 2), ('yeh', 4), ('no', 6)] The resulting dictionary is = {'hi': 1, 'hello': 2, 'yeh': 4, 'no': 6}

More Articles of Shivam Kalra:

Name Views Likes
Python program to print all the elements of binary search tree 549 17
Python program to calculate sum of k smallest elements in binary search tree. 1288 15
Python program to count binary search tree nodes that lie in a given range. 577 19
Python program to add two Matrices 484 16
Python program to check that an year is Leap Year or Not. 479 14
Python program to Convert a list of characters into a string 512 19
Python program to Transpose a Matrix 552 17
Python program to convert a List into a Tuple 528 24
Python program to find an element in binary tree. 521 17
Python program to Multiply two matrices. 628 19
Python program to covert a temperature from Celsius to Fahrenheit 484 15
Python program to print the first non-repeated character from a string 564 16
Python program to Convert a list of Tuples into Dictionary 716 26
Python program to convert a temperature from Fahrenheit to Celsius 550 12
Python program to gain understanding of Matplotlib python library 1018 18
Python program to convert Decimal to Binary, Octal and Hexadecimal. 670 23
Python program to convert set into a list 466 18
Python program to convert distance from Kilometers to Miles 518 21
Python program to count the number of nodes in Binary Search Tree. 867 23
Python program to remove duplicates from an array without using any library 957 28
Python program to Decimal to Binary using Recursion 579 24
This program is to gain understanding of Numpy python module. 664 11
Python program to show implementation of Binary Search Tree 546 22
Python program to print all the elements of binary tree 482 18
Python program to find an element in binary search tree. 1326 20
Python program to find largest number in binary search tree which is less than or equal to N 855 21
Python program to count the number of nodes in Binary Search Tree. 1328 20
Python program to print leaf nodes of the binary search tree 1648 13

Comments