How to improve this code?

Tim Chase python.list at tim.thechases.com
Tue Sep 15 04:21:34 CEST 2009


> def elementsPresent(aList, match):
>      match = set(match)
>      for item in aList:
>          if item in match:
>              return True
>      return False

This could be rewritten in Python2.5+ as

   def elementsPresent(aList, match):
     match = set(match)
     return any(elem in match for elem in aList)

though as suggested both places, the set intersection may be fastest.

-tkc






More information about the Python-list mailing list