-1

I need to check if a couple of substrings are in a list. I found the following codes. But it takes only one string.

test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] 
subs = 'Geek'
res = [i for i in test_list if subs in i] 
print ("All strings with given substring are : " + str(res)) 

What should I do if I want to find two substrings such as 'Geek' and 'Algo'.

s4fpl
  • 1
  • 3
    Does this answer your question? [Check if multiple strings exist in another string](https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string) – mrEvgenX Feb 18 '20 at 14:00
  • In question by link read `str = "a123"` as `str = ['a', '1', '2', '3']`. – mrEvgenX Feb 18 '20 at 14:01
  • Yes. Thank you! I thought I had to separate keywords. – s4fpl Feb 18 '20 at 14:19

1 Answers1

0

it will get all matches as Geek is two times matches will contain two Geek

import re 
test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] 
test_list = ' '.join(test_list)
subs = 'Geek,Algo'
matches = re.findall(r'Geek|Algo', test_list, re.IGNORECASE)
print(set(matches)
print(set((matches))

if you want to get unique just add this line

M.Shujat
  • 66
  • 3