Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading