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) |
list1 : [1, 2, 3] list2 : [8, 6, 7]
Remove common elements from two list in Python
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.