Pythonic way to compare two lists and print out the differences

前端 未结 7 1731
滥情空心
滥情空心 2021-02-07 10:52

I have two lists which are guaranteed to be the same length. I want to compare the corresponding values in the list (except the first item) and print out the ones which dont mat

7条回答
  •  执念已碎
    2021-02-07 11:11

    You can use set operation to find Symmetric difference (^) between two lists. This is one of the best pythonic ways to do this.

    list1=[1,2,3,4]
    list2=[1,5,3,4]
    
    print(set(list1) ^ set(list2))
    

    So the output will be {2, 5}

提交回复
热议问题