Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Finding Intersection

To retrieve only the values that are common to both arrays, use the intersect1d() method.

Example

Calculate the intersection of the following two set arrays.

import numpy as np

arr1 = np.array([1234])
arr2 = np.array([3456])

newarr = np.intersect1d(arr1, arr2, assume_unique=True)

print(newarr)

Note: The intersect1d() method includes an optional argument called assume_unique, which, when set to True, can enhance computation speed. It is recommended to always set this to True when working with sets.