[Solved] Python: How to Compare Two Lists


You can convert y to a set and then iterate over x to see if any of y is in it, like this

print any(any(item in word for word in x) for item in set(y))
# True

any short-circuits immediately after finding a match, so this would be very efficient.

Apart from that we can convert both of them to sets, and then check if they are disjoint sets, like this

print not {char for word in x for char in word}.isdisjoint(set(y))
# True

isdisjoint also short-circuits if it determines that both the sets are not disjoint sets.

3

solved Python: How to Compare Two Lists