How to filter a list of strings based on list of substrings in Python

1 Answer

0 votes
lst = ['java 14', 'python 3', 'php 7', 'python 4', 'php 8', 'php 9', 'java 13'] 
sublst = ['python', 'php'] 
  
def filterList(lst, sublst): 
    return [s for s in lst if any(sub in s for sub in sublst)] 
      
print(filterList(lst, sublst)) 

      


        
'''
run:
 
['python 3', 'php 7', 'python 4', 'php 8', 'php 9']
        
'''

 



answered May 1, 2020 by avibootz
edited May 1, 2020 by avibootz

Related questions

...