Short answer : use not set(a).isdisjoint(b) , it is usually the fastest.
There are four common ways to check if two lists a and b exchange any elements. The first option is to convert both to a set and check for their intersection, as such:
bool(set(a) & set(b))
Since the collection is stored using a hash table in Python, search for them O(1) (see here for more information on the complexity of operators in Python). Theoretically, this is O(n+m) on average for n and m objects in lists a and b . But 1) it must first create sets from lists, which can take a small amount of time, and 2) it assumes that hashing conflicts are scarce among your data.
The second way to do this is to use a generator expression that iterates through the lists, for example:
any(i in a for i in b)
This allows on-site searches, so no new memory is allocated for intermediate variables. He also helps out at the first find. But the in operator is always O(n) in lists (see here ).
Another proposed option is a hybrid for iterating over one of the list, converting the other into a set and testing for membership in this set, for example:
a = set(a); any(i in a for i in b)
The fourth approach is to use the isdisjoint() (frozen) sets method (see here ), for example:
not set(a).isdisjoint(b)
If the elements you are looking for are near the beginning of the array (for example, they are sorted), the generator expression is preferable, since the intersection method of the sets should allocate new memory for intermediate variables:
from timeit import timeit >>> timeit('bool(set(a) & set(b))', setup="a=list(range(1000));b=list(range(1000))", number=100000) 26.077727576019242 >>> timeit('any(i in a for i in b)', setup="a=list(range(1000));b=list(range(1000))", number=100000) 0.16220548999262974
Here's the runtime graph for this example in the list size function:

Note that both axes are logarithmic. This is the best example of expression generator. As you can see, the isdisjoint() method is better for very small list sizes, while a generator expression is better for large list sizes.
On the other hand, since the search starts from the beginning for the hybrid and generator expressions, if the shared element is systematically at the end of the array (or both lists do not have any values), intersecting and given intersection approaches are then faster than the generator expression and hybrid approach .
>>> timeit('any(i in a for i in b)', setup="a=list(range(1000));b=[x+998 for x in range(999,0,-1)]", number=1000)) 13.739536046981812 >>> timeit('bool(set(a) & set(b))', setup="a=list(range(1000));b=[x+998 for x in range(999,0,-1)]", number=1000)) 0.08102107048034668

It is interesting to note that the generator expression is slower for large list sizes. This is only for 1000 repetitions, not 100000 for the previous figure. This setting is also well approximated when no element is shared, and is the best option for non-intersecting and established intersections.
Here are two analyzes using random numbers (instead of setting up the installation in favor of a particular method):

High probability of sharing: elements are randomly taken from [1, 2*len(a)] . Low probability of sharing: items are randomly taken from [1, 1000*len(a)] .
So far, this analysis has assumed that both lists are the same size. In the case of two lists of different sizes, for example a much smaller, isdisjoint() always faster:

Make sure list a smaller; otherwise, performance will decrease. In this experiment, the size of the list a was set to 5 .
In short:
- If the lists are very small (<10 items),
not set(a).isdisjoint(b) always the fastest. - If the items in the lists are sorted or have a regular structure that you can use with, the
any(i in a for i in b) generator expression is the fastest when the list is large; - Check the intersection of the set with
not set(a).isdisjoint(b) , which is always faster than bool(set(a) & set(b)) . - Hybrid "list iteration, set test"
a = set(a); any(i in a for i in b) a = set(a); any(i in a for i in b) usually slower than other methods. - Generator expression and hybrid are much slower than the other two approaches when it comes to lists without common elements.
In most cases, using the isdisjoint() method is the best approach since the generator expression will take much longer because it is very inefficient when none of the elements are separated.