Loop between two lists until element match

I want the out_list to loop until it found a match inside in_list. i did try to increase the index if the element mismatch to make the element in out_list match with coming in_list.

in_list = [1,2,2,1,3,4]
out_list = [1,2,3,2,1,4]

index = 0
for out in out_list:
    print(out, in_list[index])
    if out == in_list[index]:
        print('match')
        index = index + 1
    else:
        print('loop until match')
        index = index + 1

The first two element for 1 and 2 in both in_list and out_list will print ‘match’. but the element for ‘3’ should iterate until it found a match. but apparently it does not.

well the expected output would be always print ‘match’ as i want it to always loop until it find a match

>Solution :

This is not an answer, but are you expecting something like this? (Ignore the print('\nNext Iteration in out_list:') line, its just for debugging)

in_list = [1,2,2,1,3,4]
out_list = [1,2,3,2,1,4]

for i in out_list:
    print('\nNext Iteration in out_list:')
    for j in in_list:
        print('  ', i, j, end=' ')
        if i == j:
            print('match')
            break
        print()
        # else:
        #     print('loop until match')

Output:


Next Iteration in out_list:
   1 1 match

Next Iteration in out_list:
   2 1 
   2 2 match

Next Iteration in out_list:
   3 1 
   3 2 
   3 2 
   3 1 
   3 3 match

Next Iteration in out_list:
   2 1 
   2 2 match

Next Iteration in out_list:
   1 1 match

Next Iteration in out_list:
   4 1 
   4 2 
   4 2 
   4 1 
   4 3 
   4 4 match

Leave a Reply