[Tutor] Customized endswith/startswith version

Negroup - negroup at gmail.com
Wed Aug 10 10:52:40 CEST 2005


Hi.

>>> f = open('codes.txt')
>>> # valid codes starts with 'abc' or '123' or 'ff5'
>>> [valid for valid in f.readlines() if valid.startswith('abc') or
valid.startswith('123') or valid.startswith('ff5')]

This is just an example, in my application I don't need to check
strings against a huge number of cases. If however it would happen,
how to modify startswith in order to make it accept a list instead of
a simple string?

[valid for valid in f.readlines() if valid.startswith(['abc', '123', 'ff5'])]

Thanks


More information about the Tutor mailing list