This is a program that takes 2 string from user converts it to list and compare the two list. If similar element found it remove it from main list

Viewed 16
def main():
    for i in range(int(input())):
        lst, lst1 = string_input()
        print(lst, lst1)
        lst2 = matcher(lst, lst1)
        print(lst2)


def string_input():
    n = input()
    n = n.split()
    lst = [int(i) for i in n]
    m = input()
    m = m.split()
    lst1 = [int(j) for j in m]
    return lst, lst1


def matcher(lst, lst1):
    lst2 = lst
    print(lst2)
    for i in lst:
        for j in lst1:
            print("lst:", i, "lst1:", j)
            if i == j:     # ------> HERE
                lst2.remove(i)
                print(lst2)
    return lst2


if __name__ == "__main__":
    main()

INPUT main list = [1, 4, 6, 8] (lst)

List to match with main list = [6, 4] (lst1)

OUTPUT IMAGE

Here it can be seen that the if statement [HERE] found a match and removed 4 form copy of main list (lst2)
but then it directly skips to main list element 8 (index 3) without checking 6 (index 2) .

0 Answers
Related