Check if any item in a list matches any item in another list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether two lists share at least one item is an intersection question, but the best implementation depends on data size, element types, and whether you need only a boolean or also the overlapping values. For tiny inputs, a simple scan is fine. For repeated or large checks, set-based membership is usually the right answer.
The Straightforward Approach
For small lists, the simplest loop is often good enough:
This is easy to read and it short-circuits on the first match. The downside is that item in list2 is itself a linear scan.
A More Pythonic Version With any()
The same idea can be written more compactly:
This is cleaner, but the runtime behavior is still basically the same as the explicit loop: each membership test against list2 is linear.
For small data, that is fine. For large lists, it becomes noticeably slower than using a set.
The Fastest General Solution: Use Sets
If you only care whether there is at least one shared value and the items are hashable, use a set:
You can also avoid building two sets if you want:
This is often the best practical approach because set membership is average-case constant time.
Which Version Should You Use?
A simple rule:
- use a loop or
any()for tiny lists or one-off scripts - use a set when the lists are large or the check happens often
For example, checking whether any blocked email appears in a user import file is a classic set-based problem:
Here, converting the blacklist to a set makes the lookup fast and natural.
When Duplicates Matter
If your only question is "is there at least one match?", duplicates do not matter. But if you need counts or the actual overlapping values, the code changes:
That tells you which distinct values overlap, but it drops duplicate counts. If multiplicity matters, use collections.Counter instead of plain sets.
Matching With Normalization
Real data often needs preprocessing before comparison. Case differences and whitespace are common examples:
If you skip normalization, your intersection logic may be technically correct but practically wrong.
Common Pitfalls
- Using sets with unhashable items. Fix: normalize complex values into hashable forms or use a different comparison approach.
- Forgetting that sets remove duplicates. Fix: use
Counteror another count-aware structure when multiplicity matters. - Ignoring case or whitespace normalization. Fix: normalize strings before comparison when business rules require it.
- Optimizing tiny one-off checks too aggressively. Fix: keep the simple loop when scale does not justify a set.
- Building two sets when one lookup set is enough. Fix: convert only one side if you only need a boolean overlap test.
Summary
- Checking whether two lists share any item is an intersection problem.
- A simple loop or
any()is easy to read and short-circuits early. - Converting one side to a set is usually the fastest general solution for large data.
- Use normalization when matches should ignore case or whitespace differences.
- If duplicates or counts matter, plain set intersection is not enough.

