Open In App
Related Articles

Remove common elements from two list in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given two lists, the task is to write a Python program to remove all the common elements of two lists. 

Examples:

Input : list1 = [1, 2, 3, 4, 5]  

          list2 = [4, 5, 6, 7, 8,]

Output : list1 = [1, 2, 3]  

              list2 = [6, 7, 8]

Explanation: Lists after removing common elements of  both the lists i.e, 4 and 5.

Input : list1 = [1, 2, 3]  

           list2 = [1, 2, 3]

Output :  list1 = []  

               list2 = []

Explanation: They have all the elements in common in 

between them.

Method 1: Using Remove() Method

The remove() method removes the first matching element (which is passed as an argument) from the list.

Python3

# Python program to remove common elements
# in the two lists using remove method
 
 
def remove_common(a, b):
 
    for i in a[:]:
        if i in b:
            a.remove(i)
            b.remove(i)
 
    print("list1 : ", a)
    print("list2 : ", b)
 
 
if __name__ == "__main__":
 
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
 
    remove_common(a, b)

                    

Output
list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

Time Complexity: O(n)
Auxiliary Space: O(1)

Method 2: Using List Comprehension

List comprehension gives a shorter syntax when you want to create a new list based on the elements of the existing list.

Python3

# Python program to remove common elements
# in the two lists using list comprehension
 
 
def remove_common(a, b):
 
    a, b = [i for i in a if i not in b], [j for j in b if j not in a]
 
    print("list1 : ", a)
    print("list2 : ", b)
 
 
if __name__ == "__main__":
 
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
 
    remove_common(a, b)

                    

Time Complexity: O(n)

Space Complexity: O(n)

Method 3: Using Set’s difference operator

The difference operator – gets items in the first set but not in the second.

Python3

# Python program to remove common elements
# in the two lists using Set’s difference
# operator
 
 
def remove_common(a, b):
 
    a, b = list(set(a) - set(b)), list(set(b) - set(a))
 
    print("list1 : ", a)
    print("list2 : ", b)
 
 
if __name__ == "__main__":
 
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
 
    remove_common(a, b)

                    

Output
list1 :  [1, 2, 3]
list2 :  [8, 6, 7]

Method 4: Using Python Set difference() Method

The difference() method in python returns a set that contains the difference between two sets i.e, the returned set contains items that exist only in the first set and excludes elements present in both sets.

Python3

# Python program to remove common elements
# in the two lists using Set difference()
# method
 
 
def remove_common(a, b):
 
    a, b = list(set(a).difference(b)), list(set(b).difference(a))
 
    print("list1 : ", a)
    print("list2 : ", b)
 
 
if __name__ == "__main__":
 
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
 
    remove_common(a, b)

                    

Output
list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

Method #5: Using Counter() Function

Python3

from collections import Counter
# Python program to remove common elements
# in the two lists
 
 
def remove_common(a, b):
    freq1 = Counter(a)
    freq2 = Counter(b)
    for key in freq1:
        if key in freq2:
            a.remove(key)
            b.remove(key)
    print("list1 : ", a)
    print("list2 : ", b)
 
 
if __name__ == "__main__":
 
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
 
    remove_common(a, b)

                    

Output
list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

Method#6: Using filterfalse() from itertools

This method uses the filterfalse() function along with the __contains__() method of sets to create new lists that contain only elements that are not present in the other list

Python3

from itertools import filterfalse
 
def remove_common(a, b):
    a ,b= list(filterfalse(set(b).__contains__, a)), list(filterfalse(set(a).__contains__, b))
    print("list1 : ", a)
    print("list2 : ", b)
if __name__ == "__main__":
 
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
 
    remove_common(a, b)

                    

Output
list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

Time complexity: O(N2).
Auxiliary space: O(1)

Another approach: Using set intersection
 

Python3

def remove_common(a, b):
  common = set(a) & set(b)
  a = [i for i in a if i not in common]
  b = [i for i in b if i not in common]
  print("list1 : ", a)
  print("list2 : ", b)
   
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
remove_common(a, b)

                    

Output
list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

Time Complexity: O(nlogn), where n is the number of elements in the list. The set intersection operation takes O(nlogn) time.
Auxiliary Space: O(n), where n is the number of elements in the list. The set requires O(n) space to store the elements.
Explanation:
We convert both the lists into sets and use the set intersection operation to find the common elements.
Then we use list comprehension to remove the common elements from both the lists and return the updated lists.



Last Updated : 28 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads