Remove a list of unwanted items from another list with list comprehension

Remove a list of unwanted item from another list with Python's list comprehensionI love list comprehensions. I really do. And it’s time for list comprehensions! If you need to withhold all items from a list1 that are present in list2 – nothing else works better!

There we go, beautiful & clean code:

list1 = ['banana', 'orange', 'apple', 'apple', 'lemon', 'strawberry', 'pineapple', 'melon', 'melon', 'peach', 'pear']
list2 = ['apple', 'lemon', pineapple]
list3 = [item for item in list1 if item not in list2]
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x