Open In App

numpy.intersect1d() function in Python

Last Updated : 17 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

numpy.intersect1d() function find the intersection of two arrays and return the sorted, unique values that are in both of the input arrays.

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.
return_indices : [bool] If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple. Default is False.

Return : [ndarray] Sorted 1D array of common and unique elements.

Code #1 :




# Python program explaining
# numpy.intersect1d() function
     
# importing numpy as geek 
import numpy as geek 
    
arr1 = geek.array([1, 1, 2, 3, 4])
arr2 = geek.array([2, 1, 4, 6])
    
gfg = geek.intersect1d(arr1, arr2)
    
print (gfg)


Output :

[1 2 4]

 
Code #2 :




# Python program explaining
# numpy.intersect1d() function
     
# importing numpy as geek 
import numpy as geek 
    
arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr2 = [1, 3, 5, 7, 9]
    
gfg = geek.intersect1d(arr1, arr2)
    
print (gfg)


Output :

[1 3 5 7 9]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads