dict1 = eval (input (" enter first dictionary: "))
dict2 = eval(input ("enter second dictionary: “))
count = 0
for i in dict1:
for j in dict2 :
if i==j:
count +=1
if count == len (dict1) :
print (” when d1 is contained in d2 : ")
else:
print ("d1 not contained in d2 ")
Can you post the error?
enter first dictionary: int
enter second dictionary: bool
Traceback (most recent call last):
File “main.py”, line 4, in
for i in dict1:
TypeError: ‘type’ object is not iterable
the indentations are not a problem
I ran the code and didn’t get any error, is this correct:
dict1 = eval (input (" enter first dictionary: "))
dict2 = eval(input ("enter second dictionary: "))
count = 0
for i in dict1:
for j in dict2:
if i==j:
count +=1
if count == len (dict1) :
print (" when d1 is contained in d2 : ")
else:
print ("d1 not contained in d2 ")
it is correct, thank you must be some problem with the software I guess
the error still persists
thank you for your kind help
Hmm, I can’t replicate the error so I don’t know what your issue is. I can say that if you are trying to check if one dict is a subset of another, there is a much easier way to do it:
d1 = {"a" : 1, "b" : 2}
d2 = {"a" : 1, "b" : 2, "c" : 3}
if d1.items() <= d2.items():
print(f"{d1} is a subset of {d2}")
Output:
{'a': 1, 'b': 2} is a subset of {'a': 1, 'b': 2, 'c': 3}
what input are you using?
Agree - this is most likely the cause of the error rather than the code.
This is the question
Program to check if a dictionary is contained in another or not.
I tried programming like:
dict1 = eval (input (" enter first dictionary: "))
dict2 = eval(input ("enter second dictionary: "))
count = 0
for i in dict1:
for j in dict2:
if i==j:
count +=1
if count == len (dict1) :
print (" when d1 is contained in d2 : ")
else:
print ("d1 not contained in d2 ")
As they said the code isn’t the problem, what are you giving the program as input?
ie: what are you entering when it asks enter first dictionary:
and enter second dictionary
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.