Categories
Python Answers

How to remove all the elements that occur in one list from another with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *