Home > front end >  Add multiple conditions with "if" where we compare all the conditions and only print faile
Add multiple conditions with "if" where we compare all the conditions and only print faile

Time:06-07

I have two lists which I extracted from two different paths and created a list:

list1 = ["123", "", "6/6/2020", "testing"]
list2 = ["6/6/2020", "testing", "hello"]

I need to compare if list1[2] == list2[0] and list1[3] == list2[1] and list1[1] == list2[2]
I want the comparison to be done on all the above conditions and only print at the end which Comparision failed. So that the user knows which value is not matching. At the end I want to print "value of {list2[2]} does not match". How to add multiple conditions with "if" where we compare all the conditions and only print at the end which condition failed in python code? Please help with this.

Thanks in advance.

CodePudding user response:

Your question is quite unclear and I am missing your own code for this. Would you need to compare all items in list1 with all items in list2? In that case it sounds like the job for a double for loop.

for each i in list1:
    for each j in list2:
        if list1[i] == list2[j]:
            do something

I will not write the code for you since this is such basic code.

CodePudding user response:

As I understand the question you want to output which value of list1 doesn't match. You're a bit unclear in the question about that.

You have

list1 = ["123", "", "6/6/2020", "testing"]
list2 = ["6/6/2020", "testing", "hello"]

Now transform list2 so that it matches the structure of list1.

list2 = [list1[0]]   [list2[2]]   list2[:2]

This will give you ['123', 'hello', '6/6/2020', 'testing'] for list2. Now comparing and printing a message for the non-match is easy:

for i, values in enumerate(zip(list1, list2)):
    if values[0] != values[1]:
        print(f'list[{i}] does not match')

Result: list[1] does not match.

  • Related