Python | Difference of two lists including duplicates
The ways to find difference of two lists has been discussed earlier, but sometimes, we require to remove only the specific occurrences of the elements as much they occur in other list. Let’s discuss certain ways in which this can be performed.
Method #1 : Using collections.Counter()
The Counter method can be used to get the exact occurrence of the elements in the list and hence can subtract selectively rather than using the set and ignoring the count of elements altogether. Then the subtraction can be performed to get the actual occurrence.
# Python3 code to demonstrate # Difference of list including duplicates # Using collections.Counter() from collections import Counter # initializing lists test_list1 = [1, 3, 4, 5, 1, 3, 3] test_list2 = [1, 3, 5] # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2)) # Using collections.Counter() # Difference of list including duplicates res = list((Counter(test_list1) - Counter(test_list2)).elements()) # print result print("The list after performing the subtraction : " + str(res)) |
The original list 1 : [1, 3, 4, 5, 1, 3, 3] The original list 2 : [1, 3, 5] The list after performing the subtraction : [1, 3, 3, 4]
Method #2 : Using map() + lambda + remove()
The combination of above functions can be used to perform this particular task. The map function can be used to link the function to all elements and remove removes the first occurrence of it. Hence doesn’t remove repeatedly. Works with Python2 only.
# Python code to demonstrate # Difference of list including duplicates # Using map() + lambda + remove() # initializing lists test_list1 = [1, 3, 4, 5, 1, 3, 3] test_list2 = [1, 3, 5] # printing original lists print("The original list 1 : " + str(test_list1)) print("The original list 2 : " + str(test_list2)) # Using map() + lambda + remove() # Difference of list including duplicates res = map(lambda x: test_list1.remove(x) if x in test_list1 else None, test_list2) # print result print("The list after performing the subtraction : " + str(test_list1)) |
The original list 1 : [1, 3, 4, 5, 1, 3, 3] The original list 2 : [1, 3, 5] The list after performing the subtraction : [1, 3, 3, 4]
Recommended Posts:
- Python | Combine two lists by maintaining duplicates in first list
- Python | Difference between two lists
- Python | String Split including spaces
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Remove all duplicates from a given string in Python
- Python | Remove Duplicates from a List
- Python | Remove duplicates from nested list
- Python | Ways to remove duplicates from list
- Python | Remove consecutive duplicates from list
- Python | Remove all duplicates words from a given sentence
- Python | Program to print duplicates from a list of integers
- Python | Sort given list by frequency and remove duplicates
- Python | Program to count duplicates in a list of tuples
- Python groupby method to remove all consecutive duplicates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



