Python
Lists Comparison
Common Elements
Programming Tutorial
Python Tips

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.

Practice algorithms

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.

python
1a = ["admin", "editor", "viewer"]
2b = ["guest", "editor"]
3
4has_overlap = not set(a).isdisjoint(b)
5print(has_overlap)

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.

python
1a = [1, 2, 3, 4]
2b = [3, 4, 5]
3
4shared = set(a) & set(b)
5print(shared)

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.

python
1def ordered_overlap(first, second):
2    lookup = set(second)
3    return [x for x in first if x in lookup]
4
5print(ordered_overlap(["x", "y", "z", "y"], ["y", "a", "z"]))

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.

python
1def list_overlap_nested(a, b):
2    a_norm = {tuple(x) for x in a}
3    b_norm = {tuple(x) for x in b}
4    return not a_norm.isdisjoint(b_norm)
5
6print(list_overlap_nested([[1, 2], [3, 4]], [[5, 6], [1, 2]]))

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.

python
1def has_overlap_stream(static_list, stream):
2    lookup = set(static_list)
3    for item in stream:
4        if item in lookup:
5            return True
6    return False
7
8stream_data = (x for x in [100, 200, 300, 4])
9print(has_overlap_stream([1, 2, 3, 4], stream_data))

This keeps memory predictable and short-circuits on first match.

Count-Based Overlap With Duplicates

If you need duplicate-aware overlap counts, use Counter.

python
1from collections import Counter
2
3a = [1, 1, 2, 3]
4b = [1, 2, 2, 4]
5
6common = Counter(a) & Counter(b)
7print(common)             # Counter({1: 1, 2: 1})
8print(sum(common.values()))

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.

python
1import numpy as np
2
3arr1 = np.array([10, 20, 30, 40])
4arr2 = np.array([40, 50, 60])
5
6shared = np.intersect1d(arr1, arr2)
7print(shared)
8print(shared.size > 0)

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.

python
1import timeit
2
3setup = "a=list(range(100000)); b=list(range(99900, 200000))"
4stmt = "not set(a).isdisjoint(b)"
5
6print(timeit.timeit(stmt=stmt, setup=setup, number=200))

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, isdisjoint and 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.