1

I have a list of items like gmail, google, outlook and another list with mx records of a domain like mail.protection.outlook.com, gmail-smtp.google.com etc. I wanted to see if the second list contains any word from the first list (even partial matches as it contains dot and hyphen). How can I achieve this?

Derik72
  • 13
  • 4
  • 1
    This is practically a duplicate of [Check if substring is in a list of strings?](https://stackoverflow.com/q/16380326/4518341), you just need to add a loop over the first list. – wjandrea Jun 22 '21 at 18:31
  • Does this answer your question? [Check if substring is in a list of strings?](https://stackoverflow.com/questions/16380326/check-if-substring-is-in-a-list-of-strings) – Vishnudev Krishnadas Jun 22 '21 at 18:33

2 Answers2

0

You can use the "in" operator like so:

fullstr = "stackoverflow"
substr = "over"

if substr in fullstr:
    print "found!"
else:
    print "not found!"
RonanCraft
  • 13
  • 6
  • But how do you do it with lists? Look at [Check if substring is in a list of strings?](https://stackoverflow.com/q/16380326/4518341) – wjandrea Jun 22 '21 at 21:02
  • just iterate through the list, check each string in the list and execute the above code, if its found, you can execute some code/break out and execute what you want to do with that information – RonanCraft Jun 23 '21 at 21:17
  • ```for I in range(list_name)``` –  May 04 '22 at 11:54
0
test_list = ["ab", "ba","abc","acb","bac"]
substr = "ab"
res = [i for i in test_list if substr in i]
Alex
  • 139
  • 6