Q. Write a Python program to create a dictionary from two lists without losing duplicate values.
Sample data
['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII'], [1, 2, 2, 3]
Expected Output: defaultdict(<class 'set'>, {'Class-V': {1}, 'Class-VI':
{2}, 'Class-VII': {2}, 'Class-VIII': {3}})
Answer =
from collections import defaultdict class_list = ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII'] id_list = [1, 2, 2, 3] temp = defaultdict (set) for c, i in zip(class_list, id_list) : temp[ c ].add ( i ) print (temp)
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )