How can I compare two lists 1 by 1 and then output false or true if they are the same

I want to take a list like ['1', '2', '3', '4'] and ['5', '6', '7', '4'] and then in order compare them 1 by 1. 1 ≠ 5 next one 2 ≠ 6, 3 ≠ 7, 4=4. I want the output to be which ones are not equal such as ['-', '-', '-', '4'].

1 answer

  • answered 2021-05-03 18:05 frab

    You can zip() and a list comprehension to compare each element pairs:

    list_1 = [0, 1, 2, 3, 4]
    list_2 = [3, 4, 5, 6, 4]
    
    lists_compared = [x if x==y else "-" for (x,y) in zip(list_1, list_2)]
    
    # output: ['-', '-', '-', '-', 4]