Saucier
Saucier

Reputation: 4360

Compare beginning of strings of a list with a whitelist

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

Answers (2)

John1024
John1024

Reputation: 113914

You can use list comprehension:

>>> [item for item in my_list if not item.startswith(whitelist)]
['three']

Upvotes: 2

smac89
smac89

Reputation: 43186

print '\n'.join([item for item in my_list if not item.startswith(whitelist)])

Upvotes: 2

Related Questions