You are currently viewing Remove Common Elements from Two Lists in Python

How to remove common elements from two lists in Python? In some cases, we need to remove elements from two lists that are common. There are several ways to remove the common elements from two lists, for example, We can directly remove the common elements from both lists using for loop and apply remove() method on both the lists. Similarly by using list comprehension. We can also utilize set difference and intersection methods and operators by converting list to set.

Advertisements

Following are methods to remove common elements from two lists-

  1. Using remove() method
  2. Using List Comprehension
  3. Using set difference operator
  4. Using set difference() method
  5. Using set intersection() method
  6. Using set intersection operator

1. Quick Examples of removing common elements from both the Lists

Following are quick examples of removing common elements from both the lists.


# Below are some quick examples.

# Initialize two lists
marks1=[10,20,45,67,90]
marks2=[10,20,15,57,90]

# Example 1: Using remove() method
for i in marks1[:]:
    if (i in marks2):
        marks1.remove(i)
        marks2.remove(i)

# Example 2: Using List Comprehension
marks1,marks2 = [i for i in marks1 if i not in marks2],
                    [j for j in marks2 if j not in marks1]

# Example 3: Using set difference operator
marks1,marks2 = list(set(marks1) - set(marks2)), list(set(marks2) - set(marks1))

# Example 4: Using set difference() method
marks1,marks2 = list(set(marks1).difference(marks2)), list(set(marks2).difference(marks1))

# Example 5: Using set intersection() method
common = set(marks1).intersection(set(marks2))
marks1 = [i for i in marks1 if i not in common]
marks2 = [i for i in marks2 if i not in common]

# Example 6: Using set intersection operator
common = set(marks1) & set(marks2)
marks1 = [i for i in marks1 if i not in common]
marks2 = [i for i in marks2 if i not in common]

2. Remove Elements from Two Lists Using Python remove() 

First, we will iterate the first list using for loop and check if the elements present in the second list or not. If the element is present, we will remove that iterated element using the list.remove() method in both lists. In this way, the common elements are eliminated from both lists.

Let’s have two lists that hold 5 integers each and remove common elements from both lists by iterating and using the remove() method.


# Create two lists
marks1=[10,20,45,67,90]
marks2=[10,20,15,57,90]
print("Original marks1: ",marks1)
print("Original marks2: ",marks2)

# Using remove() method to remove common elements
for i in marks1[:]:
    if (i in marks2):
        marks1.remove(i)
        marks2.remove(i)
        
print("Updated marks1: ",marks1)
print("Updated marks2: ",marks2)

Yields below output.

python remove common elements two lists

We can see that 10,20 and 90 are the elements that are common in both lists. So they were removed.

3. Using List Comprehension to Remove Elements in Python

With List comprehension, we will iterate the first list using the for loop and check if the iterated element is present in the second list or not. If the element is not in the first list, we are returning the element in the list and this list is assigned to the first list.

In another loop, we will iterate the second list using the for loop and check if the iterated element is present in the first list or not. If the element is not in the second list, we are returning the element in the list and this list is assigned to the second list.

3.1 Syntax

Let’s see how to iterate for loop in List Comprehension and specify the condition in the if condition.


# Syntax
list1,list2= [i for i in list1 if i not in list2],[j for j in list2 if j not in list1]

3.2 Example

Let’s have two lists that hold 5 integers each and remove common elements from both lists using List comprehension.


# Using List Comprehension to remove common elements
marks1,marks2 = [i for i in marks1 if i not in marks2],[j for j in marks2 if j not in marks1]
print(marks1,marks2)

Yields the same output as above. We can see that 10,20 and 90 are the elements that are common in both lists. So they were removed.

4. Using Set Difference Operator

The set difference will return the elements present in the first list but not present in the second list. So we can utilize the set difference operator (-) in this scenario. First, we will convert both lists to set to perform set difference operation.

Then we will subtract the first set and second set and convert the returned set to the list again with the list() method. This list is assigned to the first list.

Similarly, we will subtract the second set and first set and convert the returned set to the list again with the list() method. This list is assigned to the second list. Let’s see how to use the set difference operator.


# Syntax
list1,list2= list(set(list1) - set(list2)), list(set(list2) - set(list1))

Following is an example to remove common elements from both lists using the set difference operator.


# Using set difference operator to remove common elements
marks1,marks2 = list(set(marks1) - set(marks2)), list(set(marks2) - set(marks1))
print(marks1,marks2)

# Output:
# [45, 67] [15, 57]

5. Using Set difference() to Remove Elements from Lists

The set difference will return the elements present in the first list but not present in the second list. So we can utilize the set difference() method in this scenario. First, we will convert both lists to set to perform set difference operation.

Then we will subtract the first set and second set and convert the returned set to a list again with the list() method. This list is assigned to the first list. Parallelly, we will subtract the second set and first set and convert the returned set to the list again with the list() method. This list is assigned to the second list.

Let’s see how to use the set difference() method.


# Syntax
list(set(list1).difference(list2)), list(set(list2).difference(list1))

Following is an example to remove common elements from both the lists using the set difference() method.


# Initialize lists
marks1=[10,20,45,67,90]
marks2=[10,20,15,57,90]

# Using set difference() method
marks1,marks2 = list(set(marks1).difference(marks2)), list(set(marks2).difference(marks1))
print(marks1,marks2)

# Output:
# [45, 67] [15, 57]

6. Using Set Intersection Operator

The set intersection will return the common elements present in the sets. So we can utilize the set intersection operator (&) in this scenario. First, we will return the common elements and store them in the set. And then filter the common elements from both lists using list comprehension.


# Initialize lists
marks1=[10,20,45,67,90]
marks2=[10,20,15,57,90]

# Using set intersection operator
common = set(marks1) & set(marks2)
marks1 = [i for i in marks1 if i not in common]
marks2 = [i for i in marks2 if i not in common]
print(marks1,marks2)

# Output:
# [45, 67] [15, 57]

7. Using Set intersection() to Remove Elements from Lists in Python

The set intersection will return the common elements present in the sets similar to the set intersection operator explained above. This example uses the same approach we described with the intersection operator to remove common elements.


marks1=[10,20,45,67,90]
marks2=[10,20,15,57,90]

# Using set intersection() method
common = set(marks1).intersection(set(marks2))
marks1 = [i for i in marks1 if i not in common]
marks2 = [i for i in marks2 if i not in common]
print(marks1,marks2)

# Output:
# [45, 67] [15, 57]

8. Conclusion

We have seen several different scenarios that will remove the common elements present in both lists in python. For this, we utilized the set difference and set intersection methods and operators. list.remove() is used to remove common elements from both lists by iterating the first list using for loop and specifying the condition with if statement. List Comprehension is also used similarly to iterate both lists to return elements that are not common in both lists.