Get difference between two lists

前端 未结 27 3216
傲寒
傲寒 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条回答
  •  萌比男神i
    2020-11-21 11:52

    def diffList(list1, list2):     # returns the difference between two lists.
        if len(list1) > len(list2):
            return (list(set(list1) - set(list2)))
        else:
            return (list(set(list2) - set(list1)))
    

    e.g. if list1 = [10, 15, 20, 25, 30, 35, 40] and list2 = [25, 40, 35] then the returned list will be output = [10, 20, 30, 15]

提交回复
热议问题