Sometimes, we want to remove all the elements that occur in one list from another with Python.
In this article, we’ll look at how to remove all the elements that occur in one list from another with Python.
How to remove all the elements that occur in one list from another with Python?
To remove all the elements that occur in one list from another with Python, we can use list comprehension.
For instance, we write
l3 = [x for x in l1 if x not in l2]
to return a list of all items in l1
that isn’t in l2
.
And we assign the returned list to l3
.
Conclusion
To remove all the elements that occur in one list from another with Python, we can use list comprehension.