How to check if any item from a list of strings is in a big string?

Paul Rubin http
Thu Jul 9 23:24:54 EDT 2009


inkhorn <matt.dubins at sympatico.ca> writes:
> def list_items_in_string(list_items, string):
>     for item in list_items:
>         if item in string:
>             return True
>     return False

You could write that as (untested):

  def list_items_in_string(list_items, string):
     return any(item in string for item in list_items)

but there are faster algorithms you could use if the list is large and
you want to do the test on lots of long strings, etc.



More information about the Python-list mailing list