How can I remove the same element in the list by Python


In this article, we will show you how to remove the same element in the list in python. In simple words removing common elements from both lists.

Below are the various methods to accomplish this task −

  • Using remove() function
  • Using List Comprehension
  • Using Set difference operator
  • Using Set difference() function

Assume we have taken two lists containing some elements. We will now remove the same or common elements from both lists.

Using remove() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the inputList_1.

  • Create another variable to store the inputList_2.

  • Use the for loop, to traverse in inputList_1 by creating a copy using the [:] symbol(This will create a shallow copy of the original list while retaining all object references in the copied list).

  • Use the if conditional statement, to Check whether the element in inputList_1 is present in inputList_2 with the in operator.

  • Removing that corresponding element from both the inputList_1 & inputList_2 using the remove() method(removes the first occurrence of the element with the given value.)

  • Print the InputList_1 after removing the same or common elements.

Example

The following program removes the same or common elements from both the lists and returns the resultant lists using the remove() function.

# input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # traversing in input list 1( Here [:] is used to create shallow copy) for element in inputList_1[:]: # Checking whether the element in list1 is present in list2 if element in inputList_2: # removing that element from both the inputList_1 & inputList_2 inputList_1.remove(element) inputList_2.remove(element) # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1)

Output

On executing, the above program will generate the following output −

InputList_1 after removing same elements: [3, 1]

Using List Comprehension

The following program removes the same or common elements from both the lists and returns the resultant lists using List Comprehension.

# input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # Creating a new list containing elements that are not in the second list using the list comprehension inputList_1 = [p for p in inputList_1 if p not in inputList_2] # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1)

Output

On executing, the above program will generate the following output.

InputList_1 after removing same elements: [3, 1]

Using the Set difference operator

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a function removeSameElements() that removes the common elements from both the inputList_1, inputList_2 passed as arguments to it.

  • Convert both of the given lists to sets using the set() function(creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates), and calculate the difference between these two sets (to remove the common elements from the first list), and then convert to list using the list() function(converts the sequence/iterable to a list).

  • Print the above result.

  • Call the above defined removeSameElements() function by passing the inputList_1, inputList_2 as arguments

Example

The following program removes the same or common elements from both the lists and returns the resultant lists using the set difference operator

# creating a function that removes the common elements from # the both the inputList_1, inputList_2 passed as arguments def removeSameElements(inputList_1, inputList_2): # Converting both lists to set and calculating the set difference between the two # Converting the result back to list inputList_1 = list(set(inputList_1) - set(inputList_2)) # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1) # input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # calling the removeSameElements() function by passing both the input lists removeSameElements(inputList_1, inputList_2)

Output

On executing, the above program will generate the following output −

InputList_1 after removing same elements: [1, 3]

Using Set difference() function

In Python, the difference() method returns a set that contains the difference between two sets, i.e. the returned set comprises items that occur only in the first set and excludes items that are in both sets.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Convert the first list to a set with the set() function, then use the difference() function(The difference() method compares two sets and returns items that are unique to the first set) to compute the difference between this set and the second list, and finally convert the result to a list with the list() function.

  • Print the InputList_1 after removing the same or common elements.

Example

The following program removes the same or common elements from both the lists and returns the resultant lists using the set difference() function −

# input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # Converting the first list to set and calculate the difference between the second list using the difference() function # Converting the result back to list inputList_1 = list(set(inputList_1).difference(inputList_2)) # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1)

Output

On executing, the above program will generate the following output −

InputList_1 after removing same elements: [1, 3]

Conclusion

We learned four different ways to remove the same elements from the first list and second list in this article. We also learned how to convert a list to a set and a set to a list. We learned how to make a shallow copy and how to iterate through the iterable's shallow copy (list).

Updated on: 27-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements