Program to check whether the list B is subsequent of list A or Not
In this program,we’ll see how to check the list is subsequent or not with very easy and simple way.
A = [5,2,1,6,8] B = [2,6,1] bool = False for i in B: for j in A: if(i != j): bool = False else: bool = True break if(bool): print("List B is subsequent of A") else: print("List B is not subsequent of A")
List B is subsequent of A
In the above program we have created two list named list A and list B. we set the bool variable as False because we need to check if all the element of list B is present in list A then it (bool) will be determine that list is subsequent else not Subsequent.
We used the nested loop where outer loop iterate the element of list B and inner loop iterate the element of list A. For fist iteration i variable containing the element 2 and j variable containing 5. If the control statement ” if ” true’s then bool remain the same and j variable ‘s value will be update by 2 in its next iteration. The same process will repeat again but at this time “if statement” goes wrong hence control will enter into else part Here bool variable will be set as True and the inner for loop will be terminated due to break statement.
After that, control will goes to outer loop and execution will occurs as their conditions till the all element of list B not checked with list A.
At the end we simply checked what the value is set in variable bool. if bool contain True then list B is a subsequent of list A else Not Subsequent of List A.