0

Please explain iteration

l1=eval(input("enter list")) l2=eval(input("enter list2")) t1=tuple(l1) t2=tuple(l2) for x in l1: if x in l2: l1.remove(x) if len(t1)==0: print("True") else: print("False")

29th Jul 2022, 5:52 AM
Irshad Ahmad
Irshad Ahmad - avatar
2 ответов
+ 2
If l1=[1,2,3] l2=[1,2,3], and if len(l1)==0: then 1 will be removed from l1 during the first iteration. l1=[2,3], since the first index of l1 was checked during the first iteration, then accordingly, at the second iteration, the second index l1 will be checked, which is equal to 3, as a result, 2, which has become under the first index, will remain unchecked and l1=[2], so the result will be False. ☺️
29th Jul 2022, 6:59 AM
Solo
Solo - avatar
0
This code checks if l1 lenght equals 0 and returns boolean value. But I think it should check if l1 is sublist of l2 and returns True if it is. Code must be fixed ... for x in t1: if x in t2: l1.remove(x) print(len(l1)==0)
29th Jul 2022, 6:25 AM
Nikita M
Nikita M - avatar