Remove all values from a list present in another list

The above problem is describe that, removing all the values present in another list  i.e we are given some of the invalid numbers in one list which needs to be get ridden from the original list. So, lets’ discuss various forms to remove a list from another.

Method #1: Using list comprehension 

This list comprehension used to perform the native method in just one line and hence gives an easy method to perform this particular task.

# Python 3 code to demonstrate
# to remove elements present in other list
# using list comprehension

# initializing list
test_list = [1, 3, 4, 6, 7]

# initializing remove list
remove_list = [3, 6]

# printing original list
print ("The original list is : " + str(test_list))

# printing remove list
print ("The original list is : " + str(remove_list))

# using list comprehension to perform task
res = [i for i in test_list if i not in remove_list]

# printing result
print ("The list after performing remove operation is : " + str(res))

Output:

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

Method #2 : Using filter() + lambda

The filter function is often used alongside lambda to perform this task and creating a replacement filtered list of all the items that aren’t present within the remove Items list.

# Python 3 code to demonstrate
# to remove elements present in other list
# using filter() + lambda

# initializing list
test_list = [1, 3, 4, 6, 7]

# initializing remove list
remove_list = [3, 6]

# printing original list
print ("The original list is : " + str(test_list))

# printing remove list
print ("The original list is : " + str(remove_list))

# using filter() + lambda to perform task
res = filter(lambda i: i not in remove_list, test_list)

# printing result
print ("The list after performing remove operation is : " + str(res))

Output:

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

Method #3 : Using remove() :

remove() also can perform this task but as long as the exception of not getting specific elements is handled properly. One can iterate for all the elements of the removed list and take away those elements from the beginning list.

# Python 3 code to demonstrate
# to remove elements present in other list
# using remove()

# initializing list
test_list = [1, 3, 4, 6, 7]

# initializing remove list
remove_list = [3, 6]

# printing original list
print ("The original list is : " + str(test_list))

# printing remove list
print ("The original list is : " + str(remove_list))

# using remove() to perform task
# handled exceptions.
for i in remove_list:
	try:
		test_list.remove(i)
	except ValueError:
		pass

# printing result
print ("The list after performing remove operation is : " + str(test_list))

Output:

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

Method #4: Using set() :

set() are often used to perform this task and creating a replacement filtered list of all the elements that aren’t present within the remove element list.

# Python 3 code to demonstrate
# to remove elements present in other list
# using set()

# initializing list
test_list = [1, 3, 4, 6, 7]

# initializing remove list
remove_list = [3, 6]

# printing original list
print ("The original list is : " + str(test_list))

# printing remove list
print ("The original list is : " + str(remove_list))

# using set() to perform task
set1 = set(test_list)
set2 = set(remove_list)
res = list(set1 - set2)

# printing result
print ("The list after performing remove operation is : " + str(res))

Output:

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about this topic discussed above.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 45

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply