Test if lists share any items in python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Checking whether two Python lists share any items is common in access checks, recommendation logic, and validation pipelines. The best implementation depends on element types, data size, and whether you only need a boolean or full overlap details. This guide compares practical techniques and when to use each.
Fast Boolean Check With Sets
For hashable elements, set-based overlap is usually the fastest and clearest approach.
isdisjoint returns True when there is no overlap, so negate it when you need positive overlap check.
Retrieve Shared Items
If you need the overlapping values, use intersection.
This removes duplicates and does not preserve order.
Preserve Order and Duplicates From First List
Sometimes you need overlap while keeping order of the first list.
This retains first-list sequence and repeated values.
Handle Unhashable Elements
Set conversion fails for unhashable values such as nested lists or dicts. Normalize elements first.
For dictionaries, convert each item to a sorted tuple representation before set operations.
Streaming-Friendly Pattern
If one side is a stream and cannot be fully loaded, build lookup from the smaller static list.
This keeps memory predictable and short-circuits on first match.
Count-Based Overlap With Duplicates
If you need duplicate-aware overlap counts, use Counter.
This is useful for inventory reconciliation and similarity metrics.
Numeric Workloads With NumPy
If your pipeline already uses NumPy arrays, numpy.intersect1d is a practical option.
Use this when vectorized numeric processing is already in place.
Benchmark the Right Method for Your Data
Performance depends on list size and data distribution. Benchmark with realistic cases.
Avoid choosing implementations based only on tiny toy lists.
Common Pitfalls
- Using nested loops on large lists. Fix: use set-based membership checks for near-linear behavior.
- Expecting set intersection to preserve order. Fix: use comprehension plus lookup set when order matters.
- Applying set conversion to unhashable elements directly. Fix: normalize to immutable representations first.
- Ignoring memory cost of very large temporary sets. Fix: use streaming patterns or chunk processing when needed.
- Benchmarking with unrealistic input. Fix: test with representative data volume and shape.
Summary
- For hashable values,
isdisjointand set intersection are the best default tools. - Choose output shape first: boolean, unique overlap set, ordered list, or counts.
- Normalize unhashable items before comparison.
- Use streaming checks when one list is large or unbounded.
- Benchmark with realistic datasets before finalizing the approach.
Related reading
- Test if numpy array contains only zeros
- Tetris-ing an array
- tf.SequenceExample with multidimensional arrays
- The best shortest path algorithm
- 'tf' is not defined on load_model - using lambda
- tf.function ValueError Creating variables on a non-first call to a function decorated with tf.function, unable to understand behaviour
- The best way to calculate the height in a binary search tree? balancing an AVL-tree
- The Big O on the Dijkstra Fibonacci-heap solution

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.