The program is written in Python and it declares a list l with nine elements, each of which is a tuple of two values. The program then creates an empty dictionary named dic. The program then uses a for loop to iterate over the elements of the list l. For each iteration, the first value of the tuple is taken as the key and the second value is taken as the value. The setdefault method is used to add the key-value pair to the dictionary dic. If the key is not present in the dictionary, a new list is created for that key and the value is appended to the list. If the key is already present in the dictionary, the value is simply appended to the list for that key. Finally, the dictionary dic is passed as an argument to the print function and the dictionary is printed on the console.
l = [("Name", "Ram"), ("Name", "Pooja"), ("Name", "Sara"), ("Age", 21), ("Gender", "Male"), ("Age", 23), ("Gender", "Female"), ("Gender", "Female") , ("Age", 22)] dic = {} for x, y in l: dic.setdefault(x, []).append(y) print (dic)
{'Name': ['Ram', 'Pooja', 'Sara'], 'Age': [21, 23, 22], 'Gender': ['Male', 'Female', 'Female']}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions