I 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]