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?
print '\n'.join([item for item in my_list if not item.startswith(whitelist)])
You can use list comprehension:
>>> [item for item in my_list if not item.startswith(whitelist)]
['three']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.