Reputation: 4360
I want to compare a list with a whitelist. What I do now is the following:
>>> whitelist = ("one", "two")
>>> my_list = ["one", "two foo", "two bar", "three"]
>>> for item in my_list:
... if not item.startswith(whitelist):
... print(item)
three
Is there a more efficient/"better" way to do it?
Upvotes: 2
Views: 177
Reputation: 113914
You can use list comprehension:
>>> [item for item in my_list if not item.startswith(whitelist)]
['three']
Upvotes: 2
Reputation: 43186
print '\n'.join([item for item in my_list if not item.startswith(whitelist)])
Upvotes: 2