Python Program To Remove Common Elements From Two List

Python Program To Remove Common Elements From Two List

“How To Remove Common Elements From Two List using python” is useful concept, because sometimes programmer want to select the unique elements in first list and second list as well. So we are using for loop with if conditional statements.

Python provides a number of ways to remove common elements from two lists. The most common way is to use the built-in set() function. This function takes lists as arguments and returns a new set with unique elements. So do this process for both and subtract them and convert them back into a list.

Another way to remove common elements from two lists is to use the list.remove() method. This method removes the first occurrence of a given element from a list. So, if you iterate over one list and call list.remove() for each element in the other list, you will end up with a list that contains only the unique elements.

There are also a number of third-party libraries that provide ways to remove common elements from lists. One such library is the more-itertools library. This library provides a number of functions for working with iterators, including a function for removing common elements from two lists.

So, there are a number of ways to remove common elements from two lists in python. Which method you choose will depend on your specific needs.


01. Example

Remove the common elements from the two lists.

  1. Initialize Two List.
  2. Select one element from the first list.
  3. Then check that element available in the second list.
  4. If available in second list then remove that element from the first and second list.
def removeCommanElements(list01,list02):

	for element in list01[:]:
		if element in list02:
			list01.remove(element)
			list02.remove(element)
	
	print("nLists After Removing Common Elemets")
	print("list01 : ", list01)
	print("list02 : ", list02)

firstList = [50, 80, 30, 40, 60]
secondList = [10, 30, 50, 60, 70, 80]

print('First List:', firstList)
print('Second List:', secondList)
removeCommanElements(firstList, secondList)

Output:

First List: [50, 80, 30, 40, 60]
Second List: [10, 30, 50, 60, 70, 80]

Lists After Removing Common Elemets
list01 :  [40]
list02 :  [10, 70]

 

02. Example

Remove the common elements from the two lists.

  1. Initialize Two List.
  2. Select one element from the first list and check in second list, if available in second list then remove from first list and store it in list01.
  3. Select one element from the second list and check in first list, if available in first list then remove from second list and store it in list02.
  4. Print final first list and second list.
def removeCommanElements(list01,list02):
    list01, list02 = [i for i in list01 if i not in list02],[j for j in list02 if j not in list01]
    
    print("nLists After Removing Common Elemets")
    print("list01 : ", list01)
    print("list02 : ", list02)
    
firstList = [50, 80, 30, 40, 60]
secondList = [10, 30, 50, 60, 70, 80]

print('First List:', firstList)
print('Second List:', secondList)
removeCommanElements(firstList, secondList)

Output:

First List: [50, 80, 30, 40, 60]
Second List: [10, 30, 50, 60, 70, 80]

Lists After Removing Common Elemets
list01 :  [40]
list02 :  [10, 70]

 

03. Example

Remove the common elements from the two lists.

  1. Initialize Two List.
  2. Convert each list into a set then subtract them and again convert it into the list.
  3. Print the first list and second list.
def removeCommanElements(list01,list02):
    list01, list02 = list(set(list01) - set(list02)), list(set(list02) - set(list01))
    
    print("nLists After Removing Common Elemets")
    print("list01 : ", list01)
    print("list02 : ", list02)
    
firstList = [50, 80, 30, 40, 60]
secondList = [10, 30, 50, 60, 70, 80]

print('First List:', firstList)
print('Second List:', secondList)
removeCommanElements(firstList, secondList)

Output:

First List: [50, 80, 30, 40, 60]
Second List: [10, 30, 50, 60, 70, 80]

Lists After Removing Common Elemets
list01 :  [40]
list02 :  [10, 70]

 

Leave a comment