For short arrays, using sets is probably the clearest and most readable way to do it.

Another way is to use numpy.intersect1d. You'll have to trick it into treating the rows as a single value, though... This makes things a bit less readable...

import numpy as np

A = np.array([[1,4],[2,5],[3,6]])
B = np.array([[1,4],[3,6],[7,8]])

nrows, ncols = A.shape
dtype={'names':['f{}'.format(i) for i in range(ncols)],
       'formats':ncols * [A.dtype]}

C = np.intersect1d(A.view(dtype), B.view(dtype))

# This last bit is optional if you're okay with "C" being a structured array...
C = C.view(A.dtype).reshape(-1, ncols)

For large arrays, this should be considerably faster than using sets.

Answer from Joe Kington on Stack Overflow
🌐
IncludeHelp
includehelp.com › python › how-to-get-intersecting-rows-across-two-2d-numpy-arrays.aspx
Python - How to get intersecting rows across two 2D NumPy arrays?
Suppose that we are given two 2D numpy arrays and we need to get the intersecting (common) rows across two 2D numpy arrays.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.5 Manual
Find the intersection of two arrays · Return the sorted, unique values that are in both of the input arrays
Top answer
1 of 6
4

This should do it:

In [11]:

def f(arrA, arrB):
    return not set(map(tuple, arrA)).isdisjoint(map(tuple, arrB))
In [12]:

f(A, B)
Out[12]:
True
In [13]:

f(A, C)
Out[13]:
False
In [14]:

f(B, C)
Out[14]:
False

To find intersection? OK, set sounds like a logical choice. But numpy.array or list are not hashable? OK, convert them to tuple. That is the idea.

A numpy way of doing involves very unreadable boardcasting:

In [34]:

(A[...,np.newaxis]==B[...,np.newaxis].T).all(1)
Out[34]:
array([[False, False],
       [ True, False],
       [False, False]], dtype=bool)
In [36]:

(A[...,np.newaxis]==B[...,np.newaxis].T).all(1).any()
Out[36]:
True

Some timeit result:

In [38]:
#Dan's method
%timeit set_comp(A,B)
10000 loops, best of 3: 34.1 µs per loop
In [39]:
#Avoiding lambda will speed things up
%timeit f(A,B)
10000 loops, best of 3: 23.8 µs per loop
In [40]:
#numpy way probably will be slow, unless the size of the array is very big (my guess)
%timeit (A[...,np.newaxis]==B[...,np.newaxis].T).all(1).any()
10000 loops, best of 3: 49.8 µs per loop

Also the numpy method will be RAM hungry, as A[...,np.newaxis]==B[...,np.newaxis].T step creates a 3D array.

2 of 6
3

Using the same idea outlined here, you could do the following:

def make_1d_view(a):
    a = np.ascontiguousarray(a)
    dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(dt).ravel()

def f(a, b):
    return len(np.intersect1d(make_1d_view(A), make_1d_view(b))) != 0

>>> f(A, B)
True
>>> f(A, C)
False

This doesn't work for floating point types (it will not consider +0.0 and -0.0 the same value), and np.intersect1d uses sorting, so it is has linearithmic, not linear, performance. You may be able to squeeze some performance by replicating the source of np.intersect1d in your code, and instead of checking the length of the return array, calling np.any on the boolean indexing array.

🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.2 Manual
Find the intersection of two arrays · Return the sorted, unique values that are in both of the input arrays
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-common-values-between-two-numpy-arrays-2
Find common values between two NumPy arrays - GeeksforGeeks
July 15, 2025 - In this article, we are going to ... values, we can use the numpy.intersect1d(), which will do the intersection operation and return the common values between the 2 arrays in sorted order....
Find elsewhere
🌐
Studyopedia
studyopedia.com › home › intersection of numpy arrays
Intersection of Numpy Arrays - Studyopedia
March 30, 2026 - import numpy as np # Create two arrays n1 = np.array([10, 50, 30, 20, 60, 40]) n2 = np.array([80, 50, 90, 100, 40, 70]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Find the intersection resarr = np.intersect1d(n1, n2) print("\nIntersection (sorted result) = \n", resarr)
🌐
Plain English
python.plainenglish.io › how-to-find-an-intersection-between-two-matrices-easily-using-numpy-30263373b546
How to find an Intersection between two matrices easily using NumPy | by Sameer | Python in Plain English
February 18, 2021 - >>> import numpy as np >>> import matplotlib.pyplot as plt >>> >>> a = np.random.randint(low=0, high=2, size=(5, 5)) >>> b = np.random.randint(low=0, high=2, size=(5, 5)) >>> >>> print(a) [[1 0 1 1 0] [1 0 0 1 0] [1 0 1 1 0] [1 0 1 0 1] [0 1 0 0 0]] >>> >>> print(b) [[0 0 1 1 0] [0 0 0 0 0] [1 0 0 1 1] [0 0 1 1 0] [0 1 0 0 0]]
🌐
TutorialsPoint
tutorialspoint.com › how-to-find-intersection-between-two-numpy-arrays
Numpy intersect1d() Function
March 16, 2021 - Following is a basic example of finding the intersection of two arrays using the Numpy intersect1d() function −
🌐
GitHub
github.com › numpy › numpy › issues › 8502
Feature Request: Adding standard `settdiffXd` and `intersectXd` functions · Issue #8502 · numpy/numpy
January 19, 2017 - I think there should be a standard implementation of a settdiffXd function in numpy. Right now, various answers online are "hackish" in that they all basically convert them to 1D to end u...
Author: numpy
🌐
Data Science Parichay
datascienceparichay.com › home › blog › python – get intersection of two numpy arrays
Python - Get Intersection of Two Numpy Arrays - Data Science Parichay
June 17, 2022 - In this tutorial, we will look at how to get the intersection of elements between two numpy arrays with the help of some examples.
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.4 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v1.10 Manual
May 29, 2016 - Find the intersection of two arrays · Return the sorted, unique values that are in both of the input arrays
🌐
SciPy
docs.scipy.org › doc › numpy-1.14.0 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v1.14 Manual
Find the intersection of two arrays · Return the sorted, unique values that are in both of the input arrays
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.6.dev0 Manual
Find the intersection of two arrays · Return the sorted, unique values that are in both of the input arrays
🌐
Educative
educative.io › answers › what-is-the-numpyintersect1d-function-in-python
What is the numpy.intersect1d() function in Python?
Note: intersect1d() accepts any array-like objects; this includes NumPy arrays and scalars.
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › numpy-intersect1d-function-in-python
numpy.intersect1d() function in Python - GeeksforGeeks
May 17, 2020 - Syntax: numpy.intersect1d(arr1, arr2, assume_unique = False, return_indices = False) Parameters : arr1, arr2 : [array_like] Input arrays. assume_unique : [bool] If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.0 Manual
Find the intersection of two arrays · Return the sorted, unique values that are in both of the input arrays