Q. Here is a partially completed code that takes two lists and prints "Yes, List1 is a subset of List2" if every element of the first list is also in the second list; otherwise, the code prints "No, list1 not a subset of list2".
(Verify your code by running your complete code in Script mode)
For example if the first list is [3, 8.5, -22] and the second list is ["hello", -22, "hi", 8.5, "goblin", 3] then code would print "Yes...". On the other hand, if the first list is [3, 8.5, -22] and the second list if ["hello", -22, "hi", "goblin", 3] then the code would print "No...".
There is some code missing in this code. Your task is to supply this.
#Input lists here
for e in L1:
# Fill in the statement below
if e not in L2:
----------------------
#print statements here
Answer :-
Program :-
L2 = eval (input ("Enter the Main List2 :- ")) L1 = eval (input ("Enter the sub List1 :- ")) c = 0 for e in L1 : if e not in L2 : c += 1 if c > 0 : print ("No, list1 notasubset of list2") else : print ("Yes, List1 is a subset of List2")
Output :-
Enter the Main List2 :- ["hello", -22, "hi", 8.5, "goblin", 3]
Enter the sub List1 :- [3, 8.5, -22]
Yes, List1 is a subset of List2
>>>
Enter the Main List2 :- ["hello", -22, "hi", "goblin", 3]
Enter the sub List1 :- [3, 8.5, -22]
No, list1 not a subset of list2
>>>
Enter the Main List2 :- [1,2,3,4,5,6,7,8]
Enter the sub List1 :- [2,4,8]
Yes, List1 is a subset of List2
>>>
Enter the Main List2 :- [1,2,3,4,5,6,7,8]
Enter the sub List1 :- [1,2,9]
No, list1 not a subset of list2
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )