pythonlist-comprehension

search for elements of a list as substring in another list python


I have 2 lists. I want to find the elements in ls2 where any element of ls1 is a substring. I would like to return a list of ls2 elements along with the substring that was searched and found from ls1

ls1 = ['apple','banana','pear']
ls2 = ['strawberry is not here',
       'blueberry is over there',
       'the pear tree is not ready yet',
       'we have lots of pear trees',
       'apples are yummy']

Both of these return a partial answer

[ls1[j] for j in range(len(ls1)) if any(ls1[j] in x for x in ls2)] # returns elements from ls1 alone
[i for i in ls2 if any(w in i for w in ls1)] # returns elements from ls2 alone

What I would like is to see:

[('the pear tree is not ready yet','pear'),
 ('we have lots of pear trees','pear'),
 ('apples are yummy','apple')]

Solution

  • Get rid of the any aggregation and just do nested loops:

    result = [(sentence, word) 
              for sentence in ls2 for word in ls1 
              if word in sentence]
    

    Alternatively, use itertools.product to get the same effect as the nested loops:

    import itertools
    result = [(sentence, word) 
              for sentence, word in itertools.product(ls2, ls1) 
              if word in sentence]
    

    Try it online