Remove common elements from two list in Python
When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets.
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
# Using set operations to remove common elements
result1 = list(set(a) - set(b))
result2 = list(set(b) - set(a))
# Combine both results
c = result1 + result2
print(c)
Output
[1, 2, 3, 8, 6, 7]
Other methods that can remove common elements from two lists are:
Table of Content
Using collections.Counter
Collection.counter to count occurrences of elements in both lists and then subtracts the counters, which gives the elements that are unique to each list.
from collections import Counter
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
# Using Counter to count and subtract common elements
counter1 = Counter(a)
counter2 = Counter(b)
# Subtract the counters to remove common elements
final_result = list((counter1 - counter2).elements()) + list((counter2 - counter1).elements())
print(final_result)
Output
[1, 2, 3, 6, 7, 8]
Using List Comprehension
List comprehension is a concise and readable way to remove common elements. While it is more intuitive than sets, it can be slower for large lists because it uses the not in operator, which has linear time complexity.
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
# Using list comprehension to filter out common elements
a = [item for item in a if item not in b]
b = [item for item in b if item not in a]
print(a)
print(b)
Output
[1, 2, 3] [4, 5, 6, 7, 8]
- Each time we use not in in a list, it has to check all items in the other list, making the operation O(n) for each check. This can result in O(n*m) time complexity, where n and m are the sizes of the lists.
Using Loop
The loop method is straightforward but not very efficient. It loops over both lists and adds items to new lists if they are not common. This method is less optimal for large lists.
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
# Creating new lists to store the result
a_new = []
b_new = []
# Looping through list a and adding only non-common elements to a_new
for item in a:
if item not in b:
a_new.append(item)
# Looping through list b and adding only non-common elements to b_new
for item in b:
if item not in a:
b_new.append(item)
# Final result after removing common elements
a = a_new
b = b_new
print(a)
print(b)
Output
[1, 2, 3] [6, 7, 8]