python - break inside list comprehension -


i have datalist , filterlist , want use list comprehension method search item in datalist string contains word filterlist:

>>> datalist=['mytest123orange','dark angle','double69','spartan','broken image 999','2 cup of tea']  >>> filterlist=['test','2','x','123','orange'] >>> print [i in datalist if len([ j j in filterlist if j in i])>0 ] ['test123orange', '2 cup of tea'] 

it's working want. problem value len([ j j in filterlist if j in ])>0, need loop item inside filterlist. if match first item in filterlist, loop have go through till end. example when try check 'mytest123orannge', if test in filterlist match it's enough, want 'break' loop don't want loop rest. don't need match 'orange' or '2' or '123'.

my questions :

  1. how can break inside loop?
  2. is there other better method?

use any() generator

filterlist=['test','2','x','123','orange'] print ([i in datalist if any(j j in filterlist if j in i) ]) 

any stops iterations when first element found


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -