-2

I have two lists of strings, list_1 and list_2.

list_1 = ["Hello", "Hi", "Hey"]
list_2 = ["Hello, my name in John.", "Hi, my name is John.", "Hey, my name 
           is John.", "My name is John."]

I want to check if any of the strings in list_1 are contained in any of the strings in list_2, in such a way that returns the strings in list_2 for which this is true. Is there a good way to do this?

Dloidean
  • 59
  • 7
  • 1
    Already tried something yourself? – Elmex80s Jan 25 '19 at 11:45
  • Possible duplicate of [Fastest way to check if a value exist in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list) – Infern0 Jan 25 '19 at 11:45
  • Not clear what you are expecting to get as an output – grapes Jan 25 '19 at 11:47
  • Welcome to Stack Overflow! Do not delete your question when you get your answer. See https://meta.stackoverflow.com/questions/378440/caveat-emptor-making-students-aware-they-cannot-delete-their-homework-questions. –  Jan 25 '19 at 13:28
  • Possible duplicate of https://stackoverflow.com/q/38158577/13968392 – mouwsy Mar 24 '22 at 18:06

1 Answers1

1

You can use the any function with a generator expression as a filter condition of a list comprehension:

[s for s in list_2 if any(k in s for k in list_1)]

This returns:

['Hello, my name in John.', 'Hi, my name is John.', 'Hey, my name is John.']
blhsing
  • 91,368
  • 6
  • 71
  • 106