You can write this using a list comprehension which tells us quite literally which elements need to end up in new_list
:
a = ['apple', 'carrot', 'lemon']
b = ['pineapple', 'apple', 'tomato']
# This gives us: new_list = ['carrot' , 'lemon']
new_list = [fruit for fruit in a if fruit not in b]
Or, using a for loop:
new_list = []
for fruit in a:
if fruit not in b:
new_list.append(fruit)
As you can see these approaches are quite similar which is why Python also has list comprehensions to easily construct lists.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…