Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference of two lists including duplicates in Python
Sometimes we need to find the differences between two lists while preserving duplicates. This operation is similar to mathematical subtraction where elements from the first list are removed based on their occurrences in the second list. If an element appears multiple times, only the matching count is removed.
Using Counter from collections
The Counter class from the collections module tracks element frequencies and supports subtraction operations ?
from collections import Counter
# initializing lists
listA = ['Mon', 'Tue', 9, 3, 3]
listB = ['Mon', 3]
# printing original lists
print("Given ListA :", listA)
print("Given ListB :", listB)
# Applying collections.Counter()
diff_list = list((Counter(listA) - Counter(listB)).elements())
# Result
print("Result of list subtraction :", diff_list)
Running the above code gives us the following result −
Given ListA : ['Mon', 'Tue', 9, 3, 3] Given ListB : ['Mon', 3] Result of list subtraction : ['Tue', 9, 3]
How It Works
The Counter subtraction preserves duplicates based on frequency differences ?
from collections import Counter
listA = ['a', 'b', 'a', 'c', 'a']
listB = ['a', 'a']
print("ListA:", listA)
print("ListB:", listB)
# Show counter objects
counterA = Counter(listA)
counterB = Counter(listB)
print("Counter A:", counterA)
print("Counter B:", counterB)
# Subtraction result
result = list((counterA - counterB).elements())
print("Difference:", result)
ListA: ['a', 'b', 'a', 'c', 'a']
ListB: ['a', 'a']
Counter A: Counter({'a': 3, 'b': 1, 'c': 1})
Counter B: Counter({'a': 2})
Difference: ['a', 'b', 'c']
Alternative Method Using List Comprehension
For simple cases without complex frequency tracking, you can use list comprehension with remove() ?
def list_difference(listA, listB):
result = listA.copy()
for item in listB:
if item in result:
result.remove(item)
return result
listA = ['Mon', 'Tue', 9, 3, 3]
listB = ['Mon', 3]
diff = list_difference(listA, listB)
print("Original ListA:", listA)
print("Original ListB:", listB)
print("Difference:", diff)
Original ListA: ['Mon', 'Tue', 9, 3, 3] Original ListB: ['Mon', 3] Difference: ['Tue', 9, 3]
Comparison
| Method | Preserves Order | Performance | Best For |
|---|---|---|---|
Counter |
No | O(n + m) | Frequency-based operations |
| List Comprehension | Yes | O(n × m) | Simple cases, order preservation |
Conclusion
Use Counter from collections for efficient frequency-based list subtraction. For simple cases where order matters, list comprehension with remove() provides a straightforward alternative.
