Reputation: 61
I have the following two lists:
list_1 = ['ABC', 'DEF', 'EFG']
list_2 = ['TESTABC', 'TESTDWQ', 'TESTEFG', 'TEST123', 'TEST345']
I am using the following code in order to check if anything in list_1
is actually in list_2
:
check_list = set([item for item in list_1 for things in list_2 if item in things])
Now it works fine, it is able to tell me what it finds:
ABC
EFG
but it doesn't give me the whole value, which I am trying to get it to output:
TESTABC
TESTEFG
Is there a way to get the index or even better actually print out the value when it finds something?
Upvotes: 0
Views: 38
Reputation: 8273
Just replace item
with things
set([things for item in list_1 for things in list_2 if item in things])
To reduce it to one loop
import re
[i for i in list_2 if re.match("\w*("+'|'.join(list_1)+")$",i)]
Upvotes: 1
Reputation: 195623
My take on problem, using re
:
l1 = ['ABC', 'DEF', 'EFG']
l2 = '''TESTABC
TESTDWQ
TESTEFG
TEST123
TEST345'''
import re
s = re.findall('|'.join(f'(?:^.*{i}.*)' for i in l1), l2, flags=re.M)
print(s)
Prints:
['TESTABC', 'TESTEFG']
Upvotes: 0