Home > other >  Compare string values from two different lists at once and print the wrong value at the end
Compare string values from two different lists at once and print the wrong value at the end

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]
At the end I want to print "value of list1[1] does not match". How to write a 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