11
Question Question 11
Write a program to check if a dictionary is contained in another dictionary e.g., if
d1 = {1:11, 2:12}
d2 = {1:11, 2:12, 3:13, 4:15}
then d1 is contained in d2.
Solution
d1 = eval(input("Enter a dictionary d1: "))
d2 = eval(input("Enter a dictionary d2: "))
print("d1 =", d1)
print("d2 =", d2)
if len(d1) > len(d2):
longDict = d1
shortDict = d2
else:
longDict = d2
shortDict = d1
for key in shortDict:
if key in longDict:
if longDict[key] != shortDict[key]:
print("d1 and d2 are different ")
break
else:
print("d1 and d2 are different ")
break
else:
print(shortDict, "is contained in", longDict)Output
Enter a dictionary d1: {1:11, 2:12}
Enter a dictionary d2: {1:11, 2:12, 3:13, 4:15}
d1 = {1: 11, 2: 12}
d2 = {1: 11, 2: 12, 3: 13, 4: 15}
{1: 11, 2: 12} is contained in {1: 11, 2: 12, 3: 13, 4: 15}