Python - Difference of Two Lists Including Duplicates Last Updated : 14 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4].Using a for loopA for loop can iterate through first list and remove matching elements from second list one by one preserving duplicates. This ensures that only unmatched or extra elements remain in the result. Python a = [1, 2, 2, 3, 4] b = [2, 3] res = a.copy() for element in b: # Remove the first occurrence of each element in list2 from list1 if element in res: res.remove(element) print(res) Output[1, 2, 4] Explanation:Loop iterates through b and for each element first occurrence is removed from res using remove preserving duplicates in a.Modified list res contains elements from a after removing one instance for each match found in b which is then printedUsing collections.Countercollections.Counter counts the frequency of elements in both lists allowing easy subtraction of counts to handle duplicates resulting Counter object can then be converted back to a list representing difference including duplicates. Python from collections import Counter a = [1, 2, 2, 3, 4] b = [2, 3] # Count occurrences of elements in both lists c = Counter(a) d = Counter(b) # Subtract counts to get the difference, including duplicates res = list((c - d).elements()) print(res) Output[1, 2, 4] Explanation:Counter(a) and Counter(b) count element frequencies, and c - d subtracts counts to retain only the remaining occurrences.Elements() expands resulting counts back into a list preserving duplicates and res is printed with differenceUsing list comprehensionList comprehension iterates through a and checks if each element is in a copy of b. Matched elements are removed from the copy to handle duplicates accurately Python a = [1, 2, 2, 3, 4] b = [2, 3] c = b.copy() res = [] for element in a: # If the element is in `c`, remove it to handle duplicates if element in c: c.remove(element) else: # If not in `c`, add the element to the result res.append(element) print(res) Output[1, 2, 4] Explanation:List comprehension can be used to filter out elements from a that exist in b, while ensuring duplicates are correctly handled.By maintaining a copy of b elements are removed as they are matched ensuring each occurrence is processed accurately. Comment More infoAdvertise with us Next Article Python - Count of matching elements among lists (Including duplicates) M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - Difference of List keeping duplicates The problem of finding difference between list, i.e removing elements that occur in one list and not in other is discussed before. But the usage of sets ignores duplicates and we sometimes, require to remove the exact elements that occur in lists. Lets discuss certain ways in which this task can be 6 min read How to Find Duplicates in a List - Python Finding duplicates in a list is a common task in programming. In Python, there are several ways to do this. Letâs explore the efficient methods to find duplicates. Using a Set (Most Efficient for Large Lists)Set() method is used to set a track seen elements and helps to identify duplicates. Pythona 2 min read Difference between two Lists in Python The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4).Using setSet operations are most 3 min read Python | Difference in Record Lists Sometimes, while working with data, we may have a problem in which we require to find the difference records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list 5 min read Python - Count of matching elements among lists (Including duplicates) Given 2 lists, count all the elements that are similar in both the lists including duplicated. Input : test_list1 = [3, 5, 6, 7, 2, 3, 5], test_list2 = [5, 5, 3, 9, 8, 5] Output : 4 Explanation : 3 repeats 2 times, and 5 two times, totalling to 4. Input : test_list1 = [3, 5, 6], test_list2 = [5, 3, 4 min read Python | Combine two lists by maintaining duplicates in first list Given two lists, the task is to combine two lists and removing duplicates, without removing duplicates in original list. Example: Input : list_1 = [11, 22, 22, 15] list_2 = [22, 15, 77, 9] Output : OutList = [11, 22, 22, 15, 77, 9] Code #1: using extend Python3 # Python code to combine two lists # a 6 min read Like