Get difference between two lists

前端 未结 27 2869
傲寒
傲寒 2020-11-21 11:36

I have two lists in Python, like these:

temp1 = [\'One\', \'Two\', \'Three\', \'Four\']
temp2 = [\'One\', \'Two\']

I need to create a third

27条回答
  •  面向向阳花
    2020-11-21 11:49

    i'll toss in since none of the present solutions yield a tuple:

    temp3 = tuple(set(temp1) - set(temp2))
    

    alternatively:

    #edited using @Mark Byers idea. If you accept this one as answer, just accept his instead.
    temp3 = tuple(x for x in temp1 if x not in set(temp2))
    

    Like the other non-tuple yielding answers in this direction, it preserves order

提交回复
热议问题