from collections import Counter listA = [3, 99, 100, 4, 66, 2] listB = [2, 6, 27, 33, 96, 3] #let's assume there are no duplicated values in each list def set_difference(list1, list2): set_difference = set(list1) - set(list2) list_difference = list(set_difference) return list_difference print(set_difference(listA, listB)) print(set_difference(listB, listA)) def symmetric_difference(list1, list2): difference = set(list1).symmetric_difference(set(list2)) list_difference = list(difference) return list_difference print(symmetric_difference(listA, listB)) listC = [1, 1, 1, 2, 2, 3] listD = [1, 2, 2, 4] # if there are duplicates in lists: def counter_difference(list1, list2): difference = Counter(list1) - Counter(list2) return difference print(counter_difference(listC, listD)) print(counter_difference(listD, listC)) #symmetric difference for lists with duplicates def count_difference(list1, list2): dict1 = Counter(list1) dict2 = Counter(list2) for key, value in dict2.items(): dict1[key] = dict1.get(key, 0) - value return dict1 print(count_difference(listC, listD)) # result: Counter({1: 2, 3: 1, 2: 0, 4: -1}) means the same amount of value 2 in both lists, # for value 1 - listC contains two more of these than listD # value 3 - listC contains one more value 3 than listD # value 4 - listD contains one more value 4 than listC