Reputation: 11
How can I find a string in List1 that is a substring of any of the strings in List2? Both lists can be of different lengths.
Say I have:
List1=['hello', 'hi', 'ok', 'apple']
List2=['okay', 'never', 'goodbye']
I need it to return 'ok', seeing as it was the only string in list1 that matched list2.
Upvotes: 0
Views: 67
Reputation: 1466
I wrote this piece of code to implement the
List1=['hello', 'hi', 'ok', 'apple']
List2=['ok', 'never', 'goodbye']
i=[]
for j in List1:
for k in List2:
if j==k:
i.append(j)
print i
Upvotes: 0
Reputation: 2709
You can use list comprehension as:
[x for x in List1 for y in List2 if x in y]
Upvotes: 8
Reputation: 1150
If you want to know if a string from list1 is in list2 you can do
for s in List1:
if s in List2:
print("found s in List2")
Upvotes: 1