[Solved] Given two lists of strings, find the total number of strings in the second list which contains any string in the first list as substring


This is a simple way, but I get 4:

>>> sum(a in b for a in ListA for b in ListB)
4

Unless you want to be case-insensitive

>>> sum(a.lower() in b.lower() for a in ListA for b in ListB)
5

As stated, though, your question is ambiguous: this method counts how many matches there are. If you want to count how many words in ListB have a match, you could do this:

>>> len(set(b for a in ListA for b in ListB if a.lower() in b.lower()))
5

As an example of where it differs:

>>> ListA = ['stop', 'kill']
>>> ListB = ['stoppable', 'killable', 'stopkill']

>>> sum(a.lower() in b.lower() for a in ListA for b in ListB)
4
>>> len(set(b for a in ListA for b in ListB if a.lower() in b.lower()))
3

1

solved Given two lists of strings, find the total number of strings in the second list which contains any string in the first list as substring