In this python dictionary program, we will convert a list of Tuples into a dictionary
Steps to solve the program
- Take a list of tuples as input and create an empty list.
- Use for loop to iterate over a list of tuples.
- From tuples add 1st element as the key and 2nd element as its value to the dictionary.
- Print the output.
list1 = [("mike",1),("sarah",20),("jim", 16)]
dict1 = {}
for val in list1:
dict1[val[0]] = val[1]
print(dict1)
Output :
{'mike': 1, 'sarah': 20, 'jim': 16}